django forloop.counter operation

Fel*_*Fel 11 django for-loop django-templates

我想知道如何在django中复制它:

for photo in gallery
    if counter is 1 
        then use this photo
     endif
endfor
Run Code Online (Sandbox Code Playgroud)

如果它是"1",我怎么检查forloop计数器?

Sha*_*hin 34

{% for photo in gallery %}
    {% if forloop.counter == 1 %}
       Do something with {{ photo }}.
    {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这相当于:

{% for photo in gallery %}
    {% if forloop.first %}
       Do something with {{ photo }}.
    {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

参考:Django文档