1. You can use a filter to modify the display of variables. The filter format is {Variable | filter}. Pipeline symbol '|' indicates that a filter is used.
2. The filter can be chained, for example: {text | escape | linebreaks }}
3. The filter can also contain parameters, such:{Bio | truncatewords: 30 }}
4. If the filter parameters contain spaces, they must be enclosed by quotation marks. For example: {list | join :","}}
5. 30 built-in filters in Django
(1) Add
Format: {value | add: "2 "}}
Meaning: Increase the value by 2
(2) addslashes
Usage: {value | addslashes }}
Meaning: Add a backslash before the quotation marks in the value.
(3) capfirst
Usage: {value | capfirst }}
Meaning: the first character of value is converted into a big write form.
(4) Cut
Format: {value | cut: Arg}. For example, if the value is "string with spaces" Arg is "", the output is "stringwitheat aces"
Meaning: delete all Arg values from a given value
(5) Date
Usage ::
(A) {value | Date: "d m y"}. For example, if value is a datetime object (datetime. datetime. now (), the output will be the string "wed 09 Jan 2008"
(B) {value | date}. The format is not formatted. At this time, the formatted string will automatically adopt the format set by date_format.
Meaning: Output date format data in a given format
(6) Default
Usage: {value | default: "Nothing"}. For example, if the value is "", the output will be nothing.
Meaning: If the value indicates false, the default value is used for output.
(7) default_if_none
Usage: {value | default_if_none: "Nothing"}. For example, if the value is none, the output will be nothing.
Meaning: If the value is none, the default value is used for the output.
(8) dictsort
Meaning: If the value is a dictionary, the return value is sorted by the keyword.
Usage: {value | dictsort: "name"}, for example,
If the value is:
[{'Name': 'zed', 'age': 19}, {'name': 'amy ', 'age': 22}, {'name ': 'job', 'age': 31},]: [{'name': 'amy ', 'age': 22}, {'name ': 'job', 'age': 31}, {'name': 'zed', 'age': 19},] (9) dictsortreversed meaning: if the value of a value is a dictionary, the return value is in reverse order of the result sorted by the Keyword: Exactly the same as (8) above. (10) divisibleby usage form: {value | divisibleby: Arg}. If value is 21 and Arg is 3, the output is true. If value can be divisible by Arg, the returned value will be true (11) Escape format: {value | escape} meaning: Some Characters in value are replaced to adapt to the HTML format, including:<Is converted& Lt;>Is converted& Gt;'(Single quote) is converted& Amp; #39;"(Double quote) is converted& Quot;&Is converted& Amp; escape only works in output, so escape cannot be used in the middle of the chain filter. It should always be the last filter. If you want to use it in the middle of the chain filter, you can use force_escape (12) escapejs format: {value | escapejs}. Meaning: Some Characters in value are replaced to adapt to JavaScript and JSON formats. (13) filesizeformat format: {value | filesizeformat} meaning: format the value to make it a readable file size, such as 13kb and 4.1mb. (14) first usage form: {value | First} meaning: return the first item in the list. For example, if the value is a list ['A', 'B ', 'C'], then the output will be 'A '. (15) floatformat format: {value | floatformat} or {value | floatformat: Arg}. Arg can be a positive or negative number. Floatformat without parameters is equivalent to floatformat:-1 (1) If Arg is not included, the engine returns the result rounded up with at most one decimal point.
34.23234 |
{Value | floatformat }} |
34.2 |
34.00000 |
{Value | floatformat }} |
34 |
34.26000 |
{Value | floatformat }} |
34.3 |
(2) If Arg is a positive number, the engine rounds it and retains the ARG decimal places.
34.23234 |
{Value | floatformat: 3 }} |
34.232 |
34.00000 |
{Value | floatformat: 3 }} |
34.000 |
34.26000 |
{Value | floatformat: 3 }} |
34.260 |
(3) If Arg is a negative number, the engine returns the rounded value. If there is a fractional part, the ARG decimal is retained. Otherwise, there is no fractional part.
34.23234 |
{Value | floatformat: "-3 "}} |
34.232 |
34.00000 |
{Value | floatformat: "-3 "}} |
34 |
34.26000 |
{Value | floatformat: "-3 "}} |
34.26 |
(16) use form of get_digit: {value | get_digit: "Arg"}. For example, if value is 123456789 and Arg is 2, the output is 8 meaning: given a number, return: Number of the request. Remember: 1 indicates the rightmost number. If value is not a valid input, all original values are returned. (17) iriencodeUsage: {value | iriencode} meaning: If the value contains non-ASCII characters, it will be captured into the appropriate encoding in the URL. If the value has been urlencode, the change operation will no longer work. (18) join format: {value | join: "Arg"}. If the value is ['A', 'B', 'C'], if Arg is '//', the output is a // B // C. Meaning: Use the specified string to connect to a list, which is similar to the STR of Python. join (list) (19) Last usage form: {value | last} meaning: return the last item (20) length usage form in the list: {value | length} indicates the length of the returned value. (21) length_is format: {value | length_is: "Arg"} meaning: True is returned. If the length of value is equal to Arg, for example: if the value is ['A', 'B', 'C'], and Arg is 3, true (22) linebreaks is returned. Usage: {value | linebreaks} meaning: "\ n" in value will be replaced by <br/>, and the entire value is surrounded by </P>, so as to adapt to the HTML format (23) linebreaksbr format: {value | linebreaksbr} meaning:"\ N" in value will be replaced by <br/> (24) linenumbers usage form: {value | linenumbers} meaning: displayed text with the number of rows. (25) ljust usage form: {value | ljust} meaning: in a given width field, the value is displayed on the left alignment.(25) center usage form: {value | Center} meaning: in a given width field, the center alignment displays Value(25) use form of limit ust: {value | limit ust} meaning: the Right-aligned value is displayed in a given width field.(26) lower usage form: {value | Lower} meaning: convert a string to lowercase form (27) make_list usage form: {value | make_list} meaning: converts a value to a list. For a string, it is converted to a string list. For an integer, it is converted to an integer list. For example, if the value is Joel, the output will be [U 'J ', u'o', u'e', u'l']; if the value is 123, the output will be in the format of [, 3] (28) pluralize: {value | pluralize }}, or {value | pluralize: "es" }}, or {value | pluralize: "Y, ies"}, meaning: if the value is not 1, A Plural Suffix is returned. The default suffix is 'S' (29) random format: {value | Random} meaning: returns an arbitrary item from the given list. (30) removetags usage: {value | removetags: "tag1 tag2 tag3. .."} meaning: Delete the tag1, tag2.... tags in value. For example, if the value is<B> Joel </B> <button> is </button> A <span> slug </span> If tags is "B span", the output is:Joel <button> is </button> A slug (31) safe usage form: {value | safe} meaning: when the system is setWhen autoescaping is enabled, this filter causes the output to not undergo escape conversion (32) safeseq to be basically the same as the preceding safe, but the difference is that safe is for strings, safeseq is used for sequence (33) slice composed of multiple strings: {some_list | slice: ": 2"} meaning: it is the same as slice in Python syntax ,: 2 indicates the second element (34) of the First slugify usage form: {value | slugify} meaning: Convert the value to lower-case form. Colleagues delete all word characters, and convert the space into a horizontal line. For example, if the value is Joel is a slug, the output will be Joel-is-a-slug (35) stringformat, which is not commonly used) striptags usage: {value | striptags} meaning: delete all HTML tags in value (37) Time Format: {value | time: "H: I" }}or {value | time} meaning: format the time output. If there is no formatting parameter after time, the output is set in time_format. (38) convert a string to the title format. (39) truncatewords usage form: {value | truncatewords: 2} meaning: Cut the value into the number of words specified by truncatewords. For example, if the value is Joel is a slug, the output will be: joel is... (40) truncatewords_html uses the same form (39) meaning: If a tag is opened but not closed before the truncation point, it will be immediately closed at the truncation point. Because this operation is less efficient than truncatewords, it is only used when the value is in HTML format. (41) Upper converts a string to an uppercase string (42) urlencode (43) urlize: converts the URL in a string into a clickable form. Usage: {value | urlize}. For example, if the value isCheck out www.djangoproject.com, the output will be:Check out <a href = "http://www.djangoproject.com"> www.djangoproject.com </a> (44) urlizetrunc usage form: {value | urlizetrunc: 15} meaning: Same as (43, however, the difference is that the actual link character will be truncate into a specific length, followed... reality. (45) wordcount returns the number of words in the string (46) wordwrap usage form: {value | wordwrap: 5} meaning: Wrap the string according to the specified length, for example, if the value is Joel is a slug, the output will be: Joel is a slug (47) timesince usage form: {value | timesince: Arg} meaning: returns the number of days and hours from the ARG parameter to the value, for example,IfBlog_dateIt is a date instance, which means midnight, whileComment_dateA date instance indicates a.m. On.{Comment_date | timesince: blog_date }}Will return "8 hours ". (48) timeuntil usage form: {value | timeuntil} meaning: It is basically the same as (47). The difference is that the returned value is the number of days and hours from the current date.
Django basics-Filter