- 自定義函數
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條件