Django Template (Filter)

Source: Internet
Author: User
Tags urlencode

-------------------Django Built-in filter-------------------
1. Add
Used in the form: {{value | add: ' 2 '}}
Meaning: Increase value by 2


2, Addslashes
Using the form: {{value | addslashes}}
Meaning: Add a backslash before the quotation mark in value


3, Capfirst
Using the form: {{value | capfirst}}
Meaning: The first character of value is converted to uppercase form


4. Cut
Use the form: {{value | cut:arg}}, for example, if value is "String with spaces" Arg is "" then the output is "stringwithspaces"
Meaning: Removes the value of all arg from the given value


5. Date
The Use form is::
(a) {{value | date: "D D M Y"}}, for example, if value is a DateTime object (Datetime.datetime.now ()) then the output will be the string "Wed Jan 2008"
(b) {{value | date}}, this form does not format the string, and the formatted string is automatically in the form set by Date_format.
Meaning: Output date format data in a given format


6. Default
Use form: {{value | default: ' Nothing '}}, for example, if value is "" then the output will be nothing
Meaning: If the meaning of value is false, then the output uses the default value


7, Default_if_none
Use form: {{value | default_if_none: ' Nothing '}}, for example, if value is none, then the output will be nothing
Meaning: If value is none, then the output will use the default value

8, Dictsort
Meaning: If value is a dictionary, then the return value is the result of sorting by keyword
Use form: {{value | dictsort: ' Name '}}, for example,
If value is:
[
{' name ': ' Zed ', ' age ': 19},
{' name ': ' Amy ', ' Age ': 22},
{' name ': ' Joe ', ' Age ': 31},]
Then, the output is:
[
{' name ': ' Amy ', ' Age ': 22},
{' name ': ' Joe ', ' Age ': 31},
{' name ': ' Zed ', ' age ': 19},]


9, dictsortreversed
Meaning: If value is a dictionary, then the return value is the inverse of the result ordered by the keyword
Use form: Exactly the same as above (8).


10, Divisibleby
Use form: {{value | divisibleby:arg}}, if value is 21,arg is 3, then the output will be true
Meaning: If value can be divisible by ARG, then the return value will be true


11. Escape
Use form: {{value | escape}}
Meaning: Replace some of the characters in value to accommodate HTML formatting, including:
< is converted to &lt;
> is converted to &gt;
' (single quote) are converted to & #39;
"(double quote) is converted to &quot;
& is converted to &amp;

Note: Escape only works when output, so escape is not enough in the middle of a chain filter,
He should always be the last filter, if you want to use it in the middle of a chain filter, you can use Force_escape

12, Escapejs
Use form: {{value | escapejs}}
Meaning: Replace some of the characters in value to accommodate JavaScript and JSON formats.


13, Filesizeformat
Use form: {{value | filesizeformat}}
Meaning: Format value to make it an easy-to-read file size, for example: 13KB,4.1MB.


14. First
Use form: {{value | first}}
Meaning: Returns the first item in the list, for example, if value is a list [' A ', ' B ', ' C '], then the output will be ' a '.

15, Floatformat
Use form: {{value | floatformat}} or {{value|floatformat:arg}}, arg can be positive or negative. Floatformat with no parameters is equivalent to Floatformat:-1

1, if not with ARG, then the engine will be rounded up, and only a maximum of one decimal.
34.23234{{value|floatformat}}34.234.00000{{value|floatformat}}3434.26000{{value|floatformat}}34.3

2. If ARG is a positive number, the engine rounds and retains the decimal of the ARG bit.
34.23234{{value|floatformat:3}}34.23234.00000{{value|floatformat:3}}34.00034.26000{{Value|floatformat:3}} 34.260

3, if ARG is a negative number, then the engine will be rounded, if there is a fractional part, then the ARG bit is reserved, otherwise, there is no fractional part.
34.23234{{Value|floatformat: "-3"}}34.23234.00000{{Value|floatformat: "-3"}}3434.26000{{Value|floatformat: "-3"}} 34.26 (Get_digit)

Use form: {{value | get_digit: ' arg '}}, for example, if value is 123456789,arg is 2, then the output is 8
Meaning: Given a number, return, the number of requests, remember: 1 represents the rightmost number, and if value is not a valid input, all original values are returned.


16, Iriencode
Use form: {{value | iriencode}}
Meaning: If the value has non-ASCII characters, then it is captured into the appropriate encoding in the URL, if value has been urlencode,
Operation will no longer work.


17. Join
Use form: {{value | join: ' ARG '}}, if value is [' A ', ' B ', ' C '],arg is '//' then the output is a//b//c
Meaning: Connect a list with the specified string, acting as a python str.join (list)


18. Last
Use form: {{value | last}}
Meaning: Returns the last item in the list


19. Length
Use form: {{value | length}}
Meaning: Returns the length of the value.


20, Length_is
Use form: {{value | length_is: ' ARG '}}
Meaning: Returns True if the length of value equals arg, for example: if value is [' A ', ' B ', ' C '],arg is 3, then return True


21, linebreaks
Use form: {{Value|linebreaks}}
Meaning: "\ n" in value is replaced by <br/>, and the entire value is surrounded by </p>, and the format of the HTML is appropriate


22, LINEBREAKSBR
Use form: {{value |LINEBREAKSBR}}
Meaning: "\ n" in value will be replaced by <br/>


23, Linenumbers
Use form: {{value | linenumbers}}
Meaning: The displayed text, with the number of lines.


24, Ljust
Use form: {{value | ljust}}
Meaning: In a field of a given width, align left to display value


25. Center
Use form: {{value | center}}
Meaning: In a field of a given width, center alignment displays the value


26, Rjust
Use form: {{value | rjust}}
Meaning: In a field of a given width, right-aligned display value


27, lower
Use form: {{value | lower}}
Meaning: Convert a string to lowercase


28, Make_list
Use form: {{value | make_list}}
Meaning: Converts value to a list, to a string, to a character list, and to an integer, to an integer list
For example, if value is Joel, then the output will be [u ' J ', u ' o ', U ' e ', U ' l '];value is 123, then the output will be [all-in-one]


29, Pluralize
Usage form: {{value | pluralize}}, or {{value | pluralize: ' ES '}}, or {{value | pluralize: ' y,ies '}}
Meaning: If value is not 1, then a plural suffix is returned, and the default suffix is ' s '


30. Random
Use form: {{value | random}}
Meaning: Returns an arbitrary item from the given list


31, Removetags
Use form: {{value | removetags: "TAG1 tag2 tag3 ..."}}
Meaning: Delete value in Tag1,tag2 .... The label. For example, if value is <b>Joel</b> <button>is</button> a <span>slug</span>
tags is "b span", then the output will be: Joel <button>is</button> a slug


32. Safe
Use form: {{value | safe}}
Meaning: When the system setup autoescaping is turned on, the filter makes the output non-escape conversion


33, Safeseq
This is basically the same as safe, but one thing is different: safe is for strings, and SAFESEQ is for multiple strings sequence


34, Slice
Use form: {{some_list | slice: ': 2 '}}
Meaning: Same as slice in Python syntax: 2 represents the second element of the first


35, Slugify
Use form: {{value | slugify}}
Meaning: Convert value to lowercase, colleagues delete all sub-word characters, and turn spaces into horizontal lines
For example: If value is a slug of Joel is a, then the output will be Joel-is-a-slug


36, StringFormat
It's not used very often, but not first.


37, Striptags
Use form: {{value | striptags}}
Meaning: Delete all HTML tags in value


38. Time
Usage form: {{value | time: ' H:I '}} or {{value | time}}
Meaning: Format the time output, and if there are no formatting parameters behind it, then the output is set in Time_format.


39. Title
Converts a string into the title format.


40, Truncatewords
Use form: {{value | truncatewords:2}}
Meaning: Cut the value into truncatewords the specified number of words
For example, if value is the Joel is a slug then the output will be: Joel is ...


41, Truncatewords_html
Use form same as (39)
Meaning: If a label is open before truncation, but does not close, then the truncation point will be closed immediately.
Because this operation is less efficient than truncatewords, it is only considered when value is in HTML format.


42, Upper
Convert a string to uppercase


43, UrlEncode
To UrlEncode a string


44, Urlize
Meaning: Converts a URL in a string into a clickable form.
Use form: {{value | urlize}}
For example, if value is a check out www.djangoproject.com, then the output will be:
Check out <a href= "http://www.djangoproject.com" >www.djangoproject.com</a>


45, Urlizetrunc
Use form: {{value | urlizetrunc:15}}
Meaning: Same as (43), but a little different is that the actual link character will be truncate to a specific length, followed by ... Reality.


46, WordCount
Returns the number of words in a string


47, WordWrap
Use form: {{value | wordwrap:5}}
Meaning: Wraps the string according to the specified length
For example, if value is a slug of Joel is a, then the output will be:
Joel
is a
Slug


48, Timesince
Use form: {{value | timesince:arg}}
Meaning: Returns the number of days and hours of the parameter arg to value
For example, if Blog_date is a date instance that represents 2006-06-01 midnight, and Comment_date is a date instance representing the 2006-06-01 morning of 8 points,
Then {{Comment_date|timesince:blog_date}} will return "8 hours".


49, Timeuntil
Use form: {{value | timeuntil}}
Meaning: Similar to (47), a different point is that the value returned is the number of days and hours from the current date.

Django Template (Filter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.