【发布时间】:2011-07-13 16:06:26
【问题描述】:
我想知道如何在 django 中复制它:
for photo in gallery
if counter is 1
then use this photo
endif
endfor
如果forloop计数器为“1”,我如何检查它?
【问题讨论】:
标签: django for-loop django-templates
我想知道如何在 django 中复制它:
for photo in gallery
if counter is 1
then use this photo
endif
endfor
如果forloop计数器为“1”,我如何检查它?
【问题讨论】:
标签: django for-loop django-templates
{% for photo in gallery %}
{% if forloop.counter == 1 %}
Do something with {{ photo }}.
{% endif %}
{% endfor %}
这相当于:
{% for photo in gallery %}
{% if forloop.first %}
Do something with {{ photo }}.
{% endif %}
{% endfor %}
参考:Django docs
【讨论】: