simple_tag:
カスタムフィルタなどをsimple_tagが、より多くの柔軟なパラメータを受け取ることができます
app01 / templatetags /ディレクトリにmysimple_tag.pyを作成します
mysimple_tag.py:
Djangoのインポートテンプレートから レジスタ= template.Library() register.simple_tag @(名= "CAL") DEF CAL(ARG1、ARG2、ARG3、ARG4) 。リターン"{} + {} + {} + {}"形式(ARG1、ARG2、ARG3、ARG4)
test.htmlという。
{%負荷mysimple_tagの%} {%CAL "ABC" "DEF" "GHI" "JKL" %}
結果:
inclusion_tag:
リターンのために使用されるHTMLコード
app01 / templatetags /ディレクトリにmyinclusion_tag.pyを作成します
myinclusion_tag.py:
ジャンゴインポートテンプレートから レジスタ= template.Library() @ register.inclusion_tag( "result.html") (N)DEF show_results: N = 1の場合、N <1つの他の整数(N) データ= [ "第{}项"。範囲(1、N + 1)]におけるIのフォーマット(I) リターン{:データ"データ"}
result.html:
<UL> {データ%における選択のための%} <LI> {{選択}} </ LI> {%ENDFOR%} </ UL>
test.htmlという。
{%負荷myinclusion_tagの%} {%show_results 10%}
結果: