[Fixed]-Django forloop.counter operation

38👍

{% for photo in gallery %}
    {% if forloop.counter == 1 %}
       Do something with {{ photo }}.
    {% endif %}
{% endfor %}

this is equivalent to:

{% for photo in gallery %}
    {% if forloop.first %}
       Do something with {{ photo }}.
    {% endif %}
{% endfor %}

Reference: Django docs

Leave a comment