注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Django内建过滤器(filter)详细说明

JavaScript 水墨上仙 2243次浏览 已收录 手机上查看

django中有很多内建的过滤器,是经常可以用到的,对其进行了解可以减少很多不必要的工作。

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(1)add&nbsp&nbsp&nbsp
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式为:{{&nbspvalue&nbsp|&nbspadd:&nbsp”2″}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将value的值增加2
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(2)addslashes
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式为:{{&nbspvalue&nbsp|&nbspaddslashes&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:在value中的引号前增加反斜线
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(3)capfirst
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式为:{{&nbspvalue&nbsp|&nbspcapfirst&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:value的第一个字符转化成大写形式
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(4)cut
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式为:{{&nbspvalue&nbsp|&nbspcut:arg}},&nbsp例如,如果value是“String&nbspwith&nbspspaces”&nbsparg是”&nbsp”那么输出是”Stringwithspaces”
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:从给定value中删除所有arg的值
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(5)date
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式为::
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(a)&nbsp{{&nbspvalue&nbsp|&nbspdate:”D&nbspd&nbspM&nbspY”&nbsp}},例如,如果value是一个datetime对象(datetime.datetime.now())那么输出将是字符串”Wed&nbsp09&nbspJan&nbsp2008″
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(b)&nbsp{{&nbspvalue&nbsp|&nbspdate&nbsp}},这种形式没有格式化字符串,这时候,格式化字符串会自动采用DATE_FORMAT所设置的形式。
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将日期格式数据按照给定的格式输出
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(6)default
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspdefault:&nbsp”nothing”&nbsp}},例如,如果value是“”,那么输出将是nothing
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value的意义是False,那么输出使用缺省值
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(7)default_if_none
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspdefault_if_none:”nothing”&nbsp}},例如,如果value是None,那么输出将是nothing
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value是None,那么输出将使用缺省值
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(8)dictsort
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value的值是一个字典,那么返回值是按照关键字排序的结果
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspdictsort:”name”}},例如,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp如果value是:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp[
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’zed’,&nbsp’age’:&nbsp19},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’amy’,&nbsp’age’:&nbsp22},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’joe’,&nbsp’age’:&nbsp31},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp]
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp那么,输出是:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp[
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’amy’,&nbsp’age’:&nbsp22},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’joe’,&nbsp’age’:&nbsp31},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{‘name’:&nbsp’zed’,&nbsp’age’:&nbsp19},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp]

&nbsp&nbsp&nbsp&nbsp(9)dictsortreversed
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value的值是一个字典,那么返回值是按照关键字排序的结果的反序
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:与上述(8)完全相同。
&nbsp&nbsp&nbsp&nbsp(10)divisibleby
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspdivisibleby:arg}},如果value是21,arg是3,那么输出将是True
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value能够被arg整除,那么返回值将是True
&nbsp&nbsp&nbsp&nbsp(11)escape
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspescape}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:替换value中的某些字符,以适应HTML格式,包括:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp>&nbspis&nbspconverted&nbspto&nbsp>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp’&nbsp(single&nbspquote)&nbspis&nbspconverted&nbspto&nbsp’
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp”&nbsp(double&nbspquote)&nbspis&nbspconverted&nbspto&nbsp”
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&&nbspis&nbspconverted&nbspto&nbsp&
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspescape仅仅在输出的时候才起作用,所以escape不能够用在链式过滤器的中间,
他应该总是最后一个过滤器,如果想在链式过滤器的中间使用,那么可以使用force_escape

&nbsp&nbsp&nbsp&nbsp(12)escapejs
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspescapejs&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:替换value中的某些字符,以适应JAVASCRIPT和JSON格式。
&nbsp&nbsp&nbsp&nbsp(13)filesizeformat
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspfilesizeformat&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:格式化value,使其成为易读的文件大小,例如:13KB,4.1MB等。
&nbsp&nbsp&nbsp&nbsp(14)first
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspfirst&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:返回列表中的第一个Item,例如,如果value是列表[‘a’,’b’,’c’],那么输出将是’a’。
&nbsp&nbsp&nbsp&nbsp(15)floatformat
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspfloatformat}}或者{{value|floatformat:arg}},
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsparg可以是正数也可以是负数。没有参数的floatformat相当于floatformat:-1
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(1)如果不带arg,那么引擎会四舍五入,同时最多只保留一位小数。
34.23234 {{&nbspvalue|floatformat&nbsp}} 34.2
34.00000 {{&nbspvalue|floatformat&nbsp}} 34
34.26000 {{&nbspvalue|floatformat&nbsp}} 34.3
(2)如果arg是正数,那么引擎会四舍五入,同时保留arg位的小数。
34.23234 {{&nbspvalue|floatformat:3&nbsp}} 34.232
34.00000 {{&nbspvalue|floatformat:3&nbsp}} 34.000
34.26000 {{&nbspvalue|floatformat:3&nbsp}} 34.260
(3)如果arg是负数,那么引擎会四舍五入,如果有小数部分,那么保留arg位小数;否则,则没有任何小数部分。
34.23234 {{&nbspvalue|floatformat:”-3″&nbsp}} 34.232
34.00000 {{&nbspvalue|floatformat:”-3″&nbsp}} 34
34.26000 {{&nbspvalue|floatformat:”-3″&nbsp}} 34.26
(16)get_digit
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspget_digit:”arg”}},例如,如果value是123456789,arg是2,那么输出是8
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:给定一个数字,返回,请求的数字,记住:1代表最右边的数字,如果value不是合法输入,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp那么会返回所有原有值。
&nbsp&nbsp&nbsp&nbsp(17)iriencode
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspiriencode}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value中有非ASCII字符,那么将其进行抓化成URL中适合的编码,如果value已经进行过URLENCODE,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp改操作就不会再起作用。
&nbsp&nbsp&nbsp&nbsp(18)join
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspjoin:”arg”}},如果value是[‘a’,’b’,’c’],arg是’//’那么输出是a//b//c
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:使用指定的字符串连接一个list,作用如同python的str.join(list)
&nbsp&nbsp&nbsp&nbsp(19)last
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbsplast&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:返回列表中的最后一个Item
&nbsp&nbsp&nbsp&nbsp(20)length
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbsplength&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:返回value的长度。
&nbsp&nbsp&nbsp&nbsp(21)length_is
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbsplength_is:”arg”}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:返回True,如果value的长度等于arg的时候,例如:如果value是[‘a’,’b’,’c’],arg是3,那么返回True
&nbsp&nbsp&nbsp&nbsp(22)linebreaks
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value|linebreaks}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:value中的”\n”将被替代,并且整个value使用包围起来,从而适和HTML的格式
&nbsp&nbsp&nbsp&nbsp(23)linebreaksbr
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|linebreaksbr}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:value中的”\n”将被替代
&nbsp&nbsp&nbsp&nbsp(24)linenumbers
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsplinenumbers}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:显示的文本,带有行数。
&nbsp&nbsp&nbsp&nbsp(25)ljust
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspljust}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:在一个给定宽度的字段中,左对齐显示value
&nbsp&nbsp&nbsp&nbsp(25)center
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspcenter}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:在一个给定宽度的字段中,中心对齐显示value&nbsp
&nbsp&nbsp&nbsp&nbsp(25)rjust
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsprjust}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:在一个给定宽度的字段中,右对齐显示value
&nbsp&nbsp&nbsp&nbsp(26)lower
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsplower}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将一个字符串转换成小写形式
&nbsp&nbsp&nbsp&nbsp(27)make_list
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspmake_list}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将value转换成一个list,对于字符串,转换成字符list;对于整数,转换成整数list
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如value是Joel,那么输出将是[u’J’,u’o’,u’e’,u’l’];value是123,那么输出将是[1,2,3]
&nbsp&nbsp&nbsp&nbsp(28)pluralize
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsppluralize}},或者{{value&nbsp|&nbsppluralize:”es”}},或者{{value&nbsp|&nbsppluralize:”y,ies”}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:如果value不是1,则返回一个复数后缀,缺省的后缀是’s’
&nbsp&nbsp&nbsp&nbsp(29)random
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsprandom}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:从给定的list中返回一个任意的Item
&nbsp&nbsp&nbsp&nbsp(30)removetags
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspremovetags:”tag1&nbsptag2&nbsptag3…”}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:删除value中tag1,tag2….的标签。例如,如果value是Joel&nbspis&nbspa&nbspslug
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsptags是”b&nbspspan”,那么输出将是:Joel&nbspis&nbspa&nbspslug
&nbsp&nbsp&nbsp&nbsp(31)safe
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspsafe}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:当系统设置autoescaping打开的时候,该过滤器使得输出不进行escape转换
&nbsp&nbsp&nbsp&nbsp(32)safeseq
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp与上述safe基本相同,但有一点不同的就是:safe是针对字符串,而safeseq是针对多个字符串组成的sequence
&nbsp&nbsp&nbsp&nbsp(33)slice
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{some_list&nbsp|&nbspslice:”:2″}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:与python语法中的slice相同,:2表示第一的第二个元素
&nbsp&nbsp&nbsp&nbsp(34)slugify
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspslugify}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将value转换成小写形式,同事删除所有分单词字符,并将空格变成横线
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如:如果value是Joel&nbspis&nbspa&nbspslug,那么输出将是joel-is-a-slug
&nbsp&nbsp&nbsp&nbsp(35)stringformat
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp这个不经常用,先不说
&nbsp&nbsp&nbsp&nbsp(36)striptags
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspstriptags}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:删除value中的所有HTML标签
&nbsp&nbsp&nbsp&nbsp(37)time
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsptime:”H:i”}}或者{{value&nbsp|&nbsptime}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:格式化时间输出,如果time后面没有格式化参数,那么输出按照TIME_FORMAT中设置的进行。
&nbsp&nbsp&nbsp&nbsp(38)title
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp转换一个字符串成为title格式。
&nbsp&nbsp&nbsp&nbsp(39)truncatewords
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsptruncatewords:2}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将value切成truncatewords指定的单词数目
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如,如果value是Joel&nbspis&nbspa&nbspslug&nbsp那么输出将是:Joel&nbspis&nbsp…
&nbsp&nbsp&nbsp&nbsp(40)truncatewords_html
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式同(39)
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:truncation点之前如果某个标签打开了,但是没有关闭,那么在truncation点会立即关闭。
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp因为这个操作的效率比truncatewords低,所有只有在value是html格式时,才考虑使用。
&nbsp&nbsp&nbsp&nbsp(41)upper
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp转换一个字符串为大写形式
&nbsp&nbsp&nbsp&nbsp(42)urlencode
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp将一个字符串进行URLEncode
&nbsp&nbsp&nbsp&nbsp(43)urlize
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:将一个字符串中的URL转化成可点击的形式。
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspurlize&nbsp}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如,如果value是Check&nbspout&nbspwww.djangoproject.com,那么输出将是:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspCheck&nbspout&nbspwww.djangoproject.com
&nbsp&nbsp&nbsp&nbsp(44)urlizetrunc
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{&nbspvalue&nbsp|&nbspurlizetrunc:15}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:与(43)相同,但是有一点不同就是现实的链接字符会被truncate成特定的长度,后面以…现实。
&nbsp&nbsp&nbsp&nbsp(45)wordcount
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp返回字符串中单词的数目
&nbsp&nbsp&nbsp&nbsp(46)wordwrap
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbspwordwrap:5}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:按照指定的长度包装字符串
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如,如果value是Joel&nbspis&nbspa&nbspslug,那么输出将会是:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspJoel
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspis&nbspa
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspslug
&nbsp&nbsp&nbsp&nbsp(47)timesince
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsptimesince:arg}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:返回参数arg到value的天数和小时数
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp例如,如果&nbspblog_date&nbsp是一个日期实例表示&nbsp2006-06-01&nbsp午夜,&nbsp而&nbspcomment_date&nbsp是一个日期实例表示&nbsp2006-06-01&nbsp早上8点,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp那么&nbsp{{&nbspcomment_date|timesince:blog_date&nbsp}}&nbsp将返回&nbsp”8&nbsphours”.
&nbsp&nbsp&nbsp&nbsp(48)timeuntil
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp使用形式:{{value&nbsp|&nbsptimeuntil}}
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp意义:与(47)基本相同,一个不同点就是,返回的是value距离当前日期的天数和小时数。


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Django内建过滤器(filter)详细说明
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……