12

I would like to know how to replicate this in django:

for photo in gallery
    if counter is 1 
        then use this photo
     endif
endfor

how do I check the forloop counter if it is "1"?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.