我想知道如何在 Django 中复制它:
for photo in gallery
if counter is 1
then use this photo
endif
endfor
如果 forloop 计数器为“1”,我如何检查它?
最佳答案
{% 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
关于Django forloop.counter 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276231/