1.add
将变量与参数相加
This filter will first try to coerce both values to integers. If this fails, it’ll attempt to add the values together anyway. This will work on some data types (strings, list, etc.) and fail on others. If it fails, the result will be an empty string.
Warning: Strings that can be coerced to integers will be summed, not concatenated ,as in the first example above.
会首先将两个值强制转化为整数,转化失败再相加, 适用于字符串,列表等;如果相加失败,结果为空字符串;
注意:如果字符串可转化为数字,则按数字处理,不会进行字符串拼接;
2.addsplashes
在引号前添加反斜杠;
Adds slashes before quotes. Useful for escaping strings in CSV, for example.
{{ value|addslashes }}
If value is “I’m using Django”, the output will be “I\ 'm using Django”.
3.capfirst
Capitalizes the first character of the value. If the first character is not a letter, this filter has no effect.
首字母大写;
4.cut
Removes all values of arg from the given string
去掉给定的字符;
{{ value|cut:" " }}
# If value is "String with spaces", the output will be #"Stringwithspaces".
5.center
Centers the value in a field of a given width.
按给定长度居中;
"{{ value|center:"15" }}"
#If value is "Django", the output will be " Django ".
6.date
Formats a date according to the given format.
按给定格式格式化时间,时间表示与PHP语法兼容;
d:日; Y:年; m:月; H:24小时制; i:分钟; s:秒; c: ISO8601格式时间
r:RFC5322格式时间; U:自从1970开始的秒数;
If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string ‘Wed 09 Jan 2008’. The format passed can be one of the predefined ones DATE_FORMAT, DATETIME_FORMAT, SHORT_DATE_FORMAT or SHORT_DATETIME_FORMAT, or a custom format that uses the format specifiers shown in the table above. Note that predefined formats may vary depending on the current locale. Assuming that USE_L10N is True and LANGUAGE_CODE is, for example, “es”, then for:
{{ value|date:"SHORT_DATE_FORMAT" }}
the output would be the string “09/01/2008” (the “SHORT_DATE_FORMAT” format specifier for the es locale as shipped with Django is “d/m/Y”). When used without a format string, the DATE_FORMAT format specifier is used. Assuming the same settings as the previous example:
{{ value|date }}
outputs 9 de Enero de 2008 (the DATE_FORMAT format specifier for the es locale is r’j \d\e F \d\e Y’. You can combine date with the time filter to render a full representation of a datetime value. E.g.:
{{ value|date:"D d M Y" }} {{ value|time:"H:i" }}
注意时间对象使用time模块无效,须使用datetime模块;
7.default
If value evaluates to False, uses the given default. Otherwise, uses the value. For example:
{{ value|default:"nothing" }}
#If value is "" (the empty string), the output will be nothing
如果value评价为False,则使用default值,反之使用value本身的值;
8.default_if_none
If (and only if) value is None, uses the given default. Otherwise, uses the value. Note that if anempty string is given,the default value will not be used. Use the default filter if you want to fallback for empty strings. For example:
{{ value|default_if_none:"nothing" }}
#If value is None, the output will be nothing.
当且仅当value为None时,使用默认值.否则使用value原始的值;
9.divisibleby
Returns True if the value is divisible by the argument. For example:
{{ value|divisibleby:"3" }}
#If value is 21, the output would be True.
如果value可被参数整除,则返回True
10.escape
Escapes a string’s HTML. Specifically, it makes these replacements:
• < is converted to & lt;
• > is converted to & gt;
• ’ (single quote) is converted to & #39;
• " (double quote) is converted to & quot;
• & is converted to & amp;
Applying escape to a variable that would normally have auto-escaping applied to the result will only result in one round of escaping being done. So it is safe to use this function even in auto-escaping environments. If you want multiple escaping passes to be applied, use the force_escape filter. For example, you can apply escape to fields when autoescape is off:
{% autoescape off %}
{{ title|escape }}
{% endautoescape %}
对内容进行转义.如果在auto-escape情况下使用只会转义一次,所有escape是安全的;如果你想转义多次,那可使用force_escape过滤器.
11.filesizeformat
Formats the value like a ‘human-readable’ file size (i.e. ‘13 KB’, ‘4.1 MB’, ‘102 bytes’, etc.). For example:
{{ value|filesizeformat }}
If value is 123456789, the output would be 117.7 MB.
将文件大小转化为易读的格式;
12.first
Returns the first item in a list.
返回列表或字符串的第一个元素;
13.floatformat
转化为浮点数格式,如果不带参数,则默认保留一位小数,带参数则保留相应位数的小数;参数为0或"0"则转化为整数;参数为负数则按正数处理;(按四舍五入方式)
{{b|floatformat:3}} #保留3位小数;
{{b|floatformat:"0"}} #整数
14.get_digit
Given a whole number, returns the requested digit, where 1 is the right-most digit, 2 is the second-right-most digit, etc. Returns the original value for invalid input (if input or argument is not an integer, or if argument is less than 1).
Otherwise, output is always an integer. For example:
{{ value|get_digit:"2" }}
#If value is 123456789, the output will be 8.
返回一个整数的索引,最右边是1,倒数第二个数字是2;
15.urlencode
Escapes a value for use in a URL. For example:
{{ value|urlencode }}
If value is “https://www.example.org/foo?a=b&c=d”, the output will be “https%3A//www. example.org/foo%3Fa%3Db%26c%3Dd”. An optional argument containing the characters which should not be escaped can be provided. If not provided, the ‘/’ character is assumed safe. An empty string can be provided when all characters should be escaped. For example:
{{ value|urlencode:"" }}
If value is “https://www.example.org/”, the output will be “https%3A%2F%2Fwww.example. org%2F”.
对value进行url编码;
16.title
Converts a string into titlecase by making words start with an uppercase character and the remaining characters lowercase. This tag makes no effort to keep “trivial words” in lowercase. For example:
{{ value|title }}
#If value is "my FIRST post", the output will be "My First Post".
将value转变为标题格式,每个单词首字母大写,其余小写,但不会对琐碎的不重要单词进行处理;??
17.slice
Returns a slice of the list. UsesthesamesyntaxasPython’slistslicing. Seehttps://www.diveinto.org/python3/native-datatypes.html#slicinglists for an introduction. Example:
{{ some_list|slice:":2" }}
#If some_list is ['a', 'b', 'c'], the output will be ['a', 'b'].
对序列类型进行切片处理;
18.safe
Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.
Note: If you are chaining filters, a filter applied after safe can make the contents unsafe again. For example, the following code prints the variable as is, unescaped:
{{ var|safe|escape }}
让字符串在输出前不进行转义,在autoescaping状态下,该过滤器无影响;
19.last
Returns the last item in a list.
返回序列的最后一个元素;
20.length
Returns the length of the value. This works for both strings and lists. For example:
{{ value|length }}
#If value is ['a', 'b', 'c', 'd'] or "abcd", the output will be 4.
The filter returns 0 for an undefined variable.
返回value的长度,对字符串或列表都有效,如果是未定义变量, 返回0;
21.join
Joins a list with a string, like Python’s str.join(list) For example:
{{ value|join:" // " }}
#If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c".
将列表用参数连接,返回字符串;
22.剩余待补充…