django的模版标签和过滤器

来源:互联网 发布:java byte赋值0xff 编辑:程序博客网 时间:2024/06/07 06:33
python manage.py shellfrom django import template

if标签的使用

t = template.Template(‘{%if a%}A is true{%else%}A is False{%endif%}')t.render(template.Context({‘a’:True}))

for标签的使用

t = template.Template('{%for i in items%}<li>{{i}}</li>{%endfor%}')t.render(template.Context({‘items’:[1,3,4,5,6,6]}))

过滤器的使用:

t = template.Template(‘{{a|lower]}’).render(template.Context({‘a’:’CCCAB'}))t = template.Template(‘{{a|upper]}’).render(template.Context({‘a’:’CCCAB'}))t = template.Template(‘{{a|first]}’).render(template.Context({‘a’:’CCCAB'}))t = template.Template(‘{{a|last]}’).render(template.Context({‘a’:’CCCAB'}))t = template.Template(‘{{a|truncatechars:3]}’).render(template.Context({‘a’:’CCCAB'}))t = template.Template(‘{{a|truncatewords]}’).render(template.Context({‘a’:’CCCAB'}))
0 0