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"?
Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.17-Sept-2021.
counter indicates how many times the for tag has gone through its loop.
forloop.Allows you to test if the loop is on its last iteration.
{% 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With