這篇文章給大家分享的是有關(guān)Django中template for的用法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
當(dāng)列表為空或者非空時(shí)執(zhí)行不同操作:
{% for item in list %} ... {% empty %} ... {% endfor %}
使用forloop.counter訪(fǎng)問(wèn)循環(huán)的次數(shù),下面這段代碼依次輸出循環(huán)的次數(shù),從1開(kāi)始計(jì)數(shù):
{% for item in list %} ... {{ forloop.counter }} ... {% endfor %}
從0開(kāi)始計(jì)數(shù):
{% for item in list %} ... {{ forloop.counter0 }} ... {% endfor %}
判斷是否是第一次循環(huán):
{% for item in list %} ... {% if forloop.first %} This is the first round. {% endif %} ... {% endfor %}
判斷是否是最后一次循環(huán):
{% for item in list %} ... {% if forloop.last %} This is the last round. {% endif %} ... {% endfor %}
逆向循環(huán):
{% for item in list reversed %} {{ item }} {% endfor %}
感謝各位的閱讀!關(guān)于Django中template for的用法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!