標籤:
標籤
下面的部分概述了常見的Django標籤。
if/else
{%if%} 標籤 對一個變數值進行測試,如果結果為true,系統將會顯示在{%if%} 和 {%endif%}之間的一切,看個例子:
{% if today_is_weekend %} <p>Welcome to the weekend!</p>{% endif %} An {% else %} tag is optional:{% if today_is_weekend %} <p>Welcome to the weekend!</p>{% else %} <p>Get back to work.</p>{% endif %}
{%if%} 標籤接受 and,or,not來測試多變數,參考下面的例子:
{% if athlete_list and coach_list %} Both athletes and coaches are available.{% endif %}{% if not athlete_list %} There are no athletes.{% endif %}{% if athlete_list or coach_list %} There are some athletes or some coaches.{% endif %}{% if not athlete_list or coach_list %} There are no athletes or there are some coaches.{% endif %}{% if athlete_list and not coach_list %} There are some athletes and absolutely no coaches.{% endif %}
{%if%}標籤不接受and和or同時出現在一個標籤語句中,因為這樣會引起歧義。例如:
{% if athlete_list and coach_list or cheerleader_list %}
括弧在這裡不支援的,如果你有需要,可以考慮將邏輯放在模板的外層,並將指定的模板變數作為結果傳出來。或者嵌套{%if%}標籤:
{% if athlete_list %} {% if coach_list or cheerleader_list %} We have athletes, and either coaches or cheerleaders! {% endif %}{% endif %}
多次使用同一操作符是可以的,但同時使用多個不同的操作符是不可以的。下面的語句是有效:
{% if athlete_list or coach_list or parent_list or teacher_list %}
沒有{%elif%}標籤,用嵌套的{%if%}標籤來完成同樣的是事情:
{% if athlete_list %} <p>Here are the athletes: {{ athlete_list }}.</p>{% else %} <p>No athletes are available.</p> {% if coach_list %} <p>Here are the coaches: {{ coach_list }}.</p> {% endif %}{% endif %}
確保每個{%if%}對應一個{%endif%},否則Django將會拋出一個TemplateSyntaxError 的異常。
for
{%for%}允許你對一個序列中的每一項進行迴圈,格式是 for X in Y,Y是用來遍曆的集合,X是變數名。每次迴圈,模板系統都會將{%for%}跟{%endfor%}中內容展現出來。
例如:
<ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %}</ul>
還可以對集合進行反轉遍曆:
{% for athlete in athlete_list reversed %}...{% endfor %}
還可以進行嵌套:
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul>{% endfor %}
常用的一個模式是在迴圈遍曆之前檢查List的大小,如果List為空白則輸出一些特殊的文本
{% if athlete_list %}{% for athlete in athlete_list %}<p>{{ athlete.name }}</p>{% endfor %}{% else %}<p>There are no athletes. Only computer programmers.</p>{% endif %}
因為這個方法太過常用,for標籤支援一個可選的{%Empty%}的選項讓你定義輸出自訂的文本。例如:
{% for athlete in athlete_list %}<p>{{ athlete.name }}</p>{% empty %}<p>There are no athletes. Only computer programmers.</p>{% endfor %}
注意,for標籤不支援break,countinue。
在{%for%}標籤中,你可以訪問forloop變數,該變數有幾個常用的屬性:
1.forloop.counter:用於記錄迴圈了多少次
2.forloop.counter0:類似forloop.counter0,只不過是從0開始的。
3.forloop.revcounter:用於記錄還未被遍曆的項目數
4.forloop.revcounter0:類似revcounter,不過計數是從0開始
5.forloop.first:布爾值,用來標識是否為第一個項目
6.forloop.last:布爾值,用來表示是否為最後一個項目
ifequal/ifnotequal
{%ifequal%} 標籤比較兩個值,如果他們相等的話,顯示在{%ifequal%} 和{%endifequal%}之間的所有代碼。
{% ifequal user currentuser %}<h1>Welcome!</h1>{% endifequal %}
參數可以是寫入程式碼,所以下面的例子都是有效:
{% ifequal section ‘sitenews‘ %}<h1>Site News</h1>{% endifequal %}
{% ifequal section "community" %}
<h1>Community</h1>
{% endifequal %}
跟{%if%}類似,the {%ifequal%}標籤也支援選項{%else%}
{% ifequal section ‘sitenews‘ %}<h1>Site News</h1>{% else %}<h1>No News Here</h1>{% endifequal %}
只有模板變數、字元、整型,和浮點型可以作為對比參數,下面是一些有效例子:
{% ifequal variable 1 %}{% ifequal variable 1.23 %}{% ifequal variable ‘foo‘ %}{% ifequal variable "foo" %}
其他類型如List、字典、布爾類型的都不可以作為{%ifequal%}的參數。
{% ifequal variable True %}{% ifequal variable [1, 2, 3] %}{% ifequal variable {‘key‘: ‘value‘} %}
這些是無效的參數。如果你要測試某些東西是否為True或False,用{%ifequal%}
注釋
使用{##},多行注釋則使用{%comment%}和{%endcoomment%}
過濾器
模板過濾是在顯示他們之前變更變數值的最簡單的方法。如:{{name|lower}},這會先將name的值變為小寫,然後再顯示出來。
過濾器是可以多個接連使用的:
{{ my_list|first|upper }}
django 基本模板標籤