Is it possible to access the forloop.counter for the outermost for loop in the following template in Django:
{% for outerItem in outerItems %} {% for item in items%} <div>{{ forloop.counter }}. {{ item }}</div> {% endfor %} {% endfor %}
forloop.counter returns the innermost for loop's counter in the above example
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.
There is no break in Django template system but you can achieve an statement like break with bellow architecture. (Loop will go iteration but u don't do anything.) 1- Use with to define a variable to determine current status, 2- Use a template custom tag to change statement to negate current status.
You can use forloop.parentloop
to get to the outer forloop
, so in your case {{forloop.parentloop.counter}}
.
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