Django template system — filters

高洛峰
Release: 2016-10-17 14:08:01
Original
1078 people have browsed it

Filter, change of display form of variables

1. Form: lowercase

{{ name | lower }}


2. Concatenation: first escape the text to HTML, and then convert each line to

tag

{{ my_text|escape|linebreaks }}


3. Filter parameters

Display the first 30 words

{{ bio | truncatewords: "30" }}

Formatting

{{ pub_date | date: "F j, Y" }}


Filter list

{{ 123|add: "5" }} Add a value to value

{{ "AB'CD"|addslashes }} Single quotes plus escape marks are generally used for output to javascript

{{ "abcd"|capfirst }} The first letter is capitalized

{{ "abcd"|center:"50" }} Output specification The length of the string and center the value

{{ "123spam456spam789"|cut:"spam" }} Find and delete the specified string

{{ value|date:"F j, Y" }} Format the date

{{ value|default:"(N/A)" }} If the value does not exist, use the specified value

{{ value|default_if_none:"(N/A)" }} If the value is None, use the specified value

{ {List variable|dictsort:"Number"}} Sort from small to large

{{ List variable|dictsortreversed:"Number" }} Sort from large to small

{% if 92|pisibleby:"2" %} Determine whether Divides the specified number

{{ string|escape }} and converts it to html entity

{{ 21984124|filesizeformat }} Using 1024 as the base, calculates the maximum value, retaining 1 decimal place, increasing readability

{{ list|first }} Return the first element of the list

{{ "ik23hr&jqwh"|fix_ampersands }} & Convert to &

{{ 13.414121241|floatformat }} Retain 1 decimal place, can be a negative number, several forms

{{ 13.414121241| floatformat: "2" }} Keep 2 decimal places

{{ 23456 |get_digit: "1" }} Intercept a number at the specified position starting from the single digit

{{ list|join: ", " }} Use Specify the delimiter to connect the list

{{ list|length }} Return the number of lists

{% if list|length_is: "3" %} Whether the number of lists specifies a value

{{ "ABCD"|linebreaks }} Use New lines are marked with

,
wrapped

{{ "ABCD"|linebreaksbr }} New lines are marked with
wrapped

{{ variable|linenumbers }} Add line number to each line in the variable

{{ "abcd "|ljust:"50" }} Align the string to the left in the specified width, fill the rest with spaces

{{ "ABCD"|lower }} lowercase

{% for i in "1abc1"|make_list %}ABCDE ,{% endfor %} Use the number of characters in a string or number as a list

{{ "abcdefghijklmnopqrstuvwxyz"|phone2numeric }} Convert characters into corresponding numbers? ?

{{ list or number | pluralize }} The plural form of the word, if the number of strings in the list is greater than 1, return s, otherwise return an empty string

{{ list or number | pluralize: "es" }} Specify es

{{ list or number|pluralize:"y,ies" }} Specifies that ies is replaced by y

{{ object|pprint }} displays the value of an object

{{ list|random }} returns a random item in the list

{{ string|removetags:"br p p" }} Delete the specified html tag in the string

{{ string|rjust: "50" }} Align the string to the right in the specified width, and fill the rest with spaces

{{ list|slice:":2" }} Slice

{{ string|slugify }} The minus sign and underscore are left in the string, other symbols are deleted, and spaces are replaced with minus signs

{{ 3|stringformat:" 02i" }} String format, using Python's string format syntax

{{ "EABD"|striptags }} to strip [X]HTML syntax tags

{{ time variable|time:"P" }} date Time part format

{{ datetime|timesince }} How much time has passed since the given date until now

{{ datetime|timesince:"other_datetime" }} How much time has passed between two days

{{ datetime|timeuntil }} How much time has elapsed since a given date? The difference from the above is the position before and after the 2 dates.

{{ datetime|timeuntil:"other_datetime" }} How much time has passed between two days

{{ "abdsadf"|title }} capitalized

{{ "A B C D E F" | truncatewords: "3" }} intercepted Specify the number of words

{{ "111221"|truncatewords_html:"2" }} Intercept the specified number of html tags and complete them

  • {{ list|unordered_list }}

Multiple embeddings The set list is displayed as an unordered list of html

{{ string|upper }} All caps

linkage url encoding

{{ string|urlize }} Convert URLs from plain text to clickable links. (No experiment was successful)

{{ string|urlizetrunc: "30" }} Same as above, multiple intercepted character numbers. (Similarly no experiment was successful)

{{ "B C D E F"|wordcount }} Number of words

{{ "a b c d e f g h i j k"|wordwrap: "5" }} Insert carriage return characters after every specified number of characters

{{ boolean| yesno:"Yes,No,Perhaps" }} The return string for three values, corresponding to non-empty, empty, None


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template