首页 > 其他 > 详细

django之模版的自定义函数

时间:2017-01-09 00:36:44      阅读:258      评论:0      收藏:0      [点我收藏+]

- 自定义函数
simple_tag
a. app下创建templatetags目录
b. 任意xxoo.py文件
c. 创建template对象 register
d.

__author__ = ‘Administrator‘
from django import template
from django.utils.safestring import mark_safe

register = template.Library()
@register.simple_tag

def func(a1,a2,a3....)
return "asdfasd"
e. settings中注册APP
f. 顶部 {% load xxoo %}
g. {% 函数名 arg1 arg2 %}
缺点:
不能作为if条件

优点:
参数任意

 

 

filter
a. app下创建templatetags目录
b. 任意xxoo.py文件
c. 创建template对象 register
d.
@register.filter
def func(a1,a2)
return "asdfasd"
e. settings中注册APP
f. 顶部 {% load xxoo %}
g. {{ 参数1|函数名:"参数二,参数三" }} {{ 参数1|函数名:数字 }}
缺点:
最多两个参数,不能加空格
优点:
能作为if条件

 

django之模版的自定义函数

原文:http://www.cnblogs.com/liruixin/p/6263119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!