Commonly used filter comes django

filter   description  Usage Example result
upper  In uppercase output {{  laobian | upper   }} LAOBIAN
Socket usage

An output filter but also as an input conduit at a conduit

{{ [a,b,c] | first | upper }}  [ A,B,C ]
lower  In lowercase mode output {{ HELLO | lower }}  hello

add

This filter will attempt to integer values ​​and the parameter is then loaded into the addition, for example: the value is 2, then the result is 7, if the conversion process fails, then the parameter value will be spliced ​​string Ruoguo such as "abc", then it will become "adc5", if the list will be spliced ​​into a list.

{{ 2 | add: "5" }}

 

{{ "abc" | add:"5" }}

 

{{ [ a,b,c ] | add: "5"}}

7

 

"adc5"

 

[a,b,c,5]

cut  Removal value specified as a string {{ “hello” | cut:“h” }}   "ello "
date j which a date in the specified format, formatted string {{ birthday | date: " Y/m/d " }}  
default  If the value is evaluated to False. Such as [], "", None, {}, etc. These are False value will use the default filter is determined if the default values ​​provided {{ value | default: " nothing " }}  
first Returns the first element of the list / tuple / string.  {{ value | first }}  
last  Returns the last element of the list / tuple / string {{ value | last }}  
floatformat Use a rounded way to format floating point type. If the filter does not pass any parameters. Then only one decimal after the decimal point, if behind the decimal are all 0, then only reserved integer. Of course, also pass a parameter to identify a specific reserve several decimals. 1. If no arguments are passed:

| Value | template codes | output | | --- | --- | --- | |

No parameters:

34.23234 | {{ value\|floatformat }} | 34.2 | |

34.000 | {{ value\|floatformat }} | 34 | |

34.260 | {{ value\|floatformat }} | 34.3 |

There are parameters:

34.23234 | {{value\|floatformat:3}} | 34.232 | |

34.0000 | {{value\|floatformat:3}} | 34.000 | |

34.26000 | {{value\|floatformat:3}} | 34.260 |
 

 
join  Python is similar to the join, a list / tuple / string spliced ​​with the specified character {{ value | join:"/" }}  
length Get a list of length / tuple / string / dictionaries {{ value | length }}  
random  Being a random list / string / select a value tuples. {{ value | random}}  
safe  Mark a string is safe. That will turn off automatically escapes the string {{ value | safe }} If the value is a string that does not contain any special characters, such as <a> this, the above code will be the normal input string. If the value is a string of html code, then the above code will render this html code into your browser
slice  Slicing operation is similar in Python {{ value | slice :" 2" }}  
stringtags Remove all html tags string. {{ value | stringtags }}  
truncatechars 如果给定的字符串长度超过了过滤器指定的长度。那么就会进行切割,并且会拼接三个点来作为省 略号 {{ value | truncatechars:"5"}} 如果 value 是等于 北京欢迎您~ ,那么输出的结果是 北京... 。可能你会想,为什么不会 北京欢迎 您... 呢。因为三个点也占了三个字符,所以 北京 +三个点的字符长度就是5。
truncatechars_html 类似于 truncatechars ,只不过是不会切割 html 标签   如果 value 是等于<p>北京欢迎您~</p>,那么输出将是<p>北京...</p>.
       

Guess you like

Origin blog.csdn.net/weixin_43567965/article/details/89458850