内置过滤器
目的是对绝大多数的内置过滤器进行测试总结学习,现设置如下:
urls.py中设置:
urlpatterns=[
url(r'^method',views.method,name='method'),
url(r'^$',TemplateView.as_view(template_name='banners/test.html',extra_context={'variable':'hello',other:'something'}),name='index'),
]
test.html中设置:
{{ variable|filter:args }}
Strings,Numbers,Lists
- add
'variable':'hello'
{{ variable|add:' world' }}
'variable':'3'
{{ variable|add:' 4' }}
':variable':[1,2,3], 'other':[4,5]
{{ variable|add:other }}
- default
如果变量是false,不存在,或者空字符串,则用该过滤器来提供了一个缺省值.
':variable':''
{{ variable|default:'i am default' }}
':variable':False
{{ variable|default:'i am default' }}
':variable':False
{{ variablesss|default:'i am default' }}
- default_if_none
仅对变量是None的进行缺省设置,对于空字符,False,不存在的不进行处理.
':variable':None
{{ variable|default_if_none:'i am default' }}
'variable':False
{{ variable|default_if_none:'i am default' }}
'variable':False
{{ variablssse|default_if_none:'i am default' }}
- length
'variable':[1,2,3]
{{ variable|length }}
'variable':'abcd'
{{ variable|length }}
'variable':123
{{ variable|length }}
-
length_is
'variable':'abcd'
{{ variable|length_is:4 }}
或者{{ variable|length_is:'4' }}
-
make_list
'variable':'1234'
{{ variable|make_list }}
'variable':1234
{{ variable|make_list }}
- yesno
yesno过滤器将True,False,None,映射为yes,no,maybe
'variable':True
{{ variable|yesno}}
Numbers
- divisibleby
变量可被过滤器后参数整除,返回True,否则返回False,过滤器参数可为例如5,'5'.
'variable':20
{{ variable|divisibleby:'4' }}
'variable':'20'
{{ variable|divisibleby:'4' }}
- filesizeformat
'variable':'250'
{{ variable|filesizeformat }}
'variable':200000
{{ variable|filesizeformat }}
-
floatformat
对浮点数小数点后的数字进行round,过滤器后参数可正可负,基本功能都一样,但对于比如9.000这样的数字而言,floatformat:3是9.000.而floatformat:-3是,默认是-1.
':variable':'3.1415'
{{ variable|filesizeformat }}
-
get_digit
获取数字某一位的值,1是最后一个,如果过滤器参数小于1,或者不是整数,那么输出全部数字,如果超过了位数,输出0.
'variable':12345
{{ variable|get_digit:3 }}
Strings
- capfirst
':variable':'hello world'
{{ variable|capfirst }}
- cut:
将string变量中出现的cut后的参数全部去除,返回剩下的部分
'variable':'asbscs’
{{ variable|cut:'s' }}
'variable':'a b c'
{{ variable|cut:' ' }}
- linenumbers
'variable':"""a
b
c
d"""
{{ variable|cut:' ' }}
- lower
将字符串变量的大写字母全部变成小写字母
"variable":"Hello World"
{{ variable|lower }}
- stringformat
对于python,有 '%3d' % 7--->'007'
同样的,去掉'%',
"variable":7
{{ variable|stringformat:'3d' }}
- title
"variable":'hellor'
{{ variable|title }}
- slugify
"variable":'hellor yes no'
{{ variable|slugify }}
- truncatechars
"variable":'hellosjfdkls'
{{ variable|trancatechars:4 }}
- truncatechar_html
"variable":'hellosjfdkls'
{{ variable|trancatechars_html:4 }}
- truncatewords
"variable":'coffeehouse started as a small store'
{{ variable|trancatewords:3 }}
- truncatewords_html
"variable":'coffeehouse started as a small store'
{{ variable|trancatewords:3 }}
- upper
"variable":'coffeehouse started as a small store'
{{ variable|upper }}
- wordcount
"variable":'coffeehouse started as a small store'
{{ variable|wordcount }}
Lists and Dictionaries
- dictsort
'variable':[{'name':'Downtown','city':'san diego'},{'name':'Uptown','city':'san Diego'},{'name':'Midtown','city':'san Diego'},{'name':'Atown','city':'san Diego'}]
{% with newdict=variable|dictsort:'name' %}
{{ newdict }}
{% endwith %}
上例,对variable中的各dict中的'name'进行排序(按照字母),赋值给newdict后,显示newdict,当然也可以直接{{ variable|dictsort:'name'}}
dictsort过滤器也可以通过指明一个index编号来对包含多个元组的列表进行操作,比如{% with otherlist=listoftuples|dictsort:0 %}就是通过每个元组的第一个元素进行了重排。
- dictsortreversed
'variable':[{'name':'Downtown','city':'san diego'},{'name':'Uptown','city':'san Diego'},{'name':'Midtown','city':'san Diego'},{'name':'Atown','city':'san Diego'}]
{% with newdict=variable|dictsortreversed:'name' %}
{{ newdict }}
{% endwith %}
- join
就像python 列表的join方法一样。
'variable':['a','e','i']
{{ variable|join:'--'}}
- first,last,random
返回变量列表的第一个,最后一个,随机的item
- slice
就像列表的slice操作
'variable':['a','e','i','o','u']
{{ variable|slice:':3' }}
- unordered_list
'variable':['Stores',['san diego',['Downtown','Uptown','Midtown']]]
{{ variable|unordered_list }}
Spacing and special characters
- addslashed
对所有的引号进行添加斜
'variable':"today's news"
{{ variable|addslashes }}
- ceneter
'variable':"1234567890111213"
{{ variable|center:'15' }}
-
ljust,rjust
-
linebreaks
'variable':"Main
San,CA"
{{ variable|linebreaks }}
- linebreaksbr
'variable':"Main
San,CA"
{{ variable|linebreaksbr }}
linebreakbr与linebreak的区别是:前者只把
转换为
,后者还附带了
,
- safe
若变量为yes,输出的仍然是本身,如果加safe过滤器,则表明信任该变量,允许渲染。
'variable':"yes"
{{ variable|safe }}
- safeseq
'variable':['Yes','OK']
{{ variable|safeseq|join:',' }}
Url
- urlencode
'variable':'http://localhost/drinks?type=cold&size=large'
{{ variable|urlencode }}
- urlize
使得链接可被点击
'variable':'https://www.midasuser.cn/index.html'
{{ variable|urlize }}
- urlizetrunc
'variable':'https://www.midasuser.cn/index.html'
{{ variable|urlizetrunc:10 }}