Answer the question
In order to leave comments, you need to log in
Is it possible to get the forloop.counter of the outer loop?
I use Django-Mptt.
There is a nested loop, I need to get the counter value of the outer loop in the nested loop
<table class="table">
{% for category in categorys %}
{% if category.is_leaf_node %}
<tr class="treegrid-{{ forloop.counter }}">
<td>{{ category.name }}</td>
</tr>
{% for product in category.get_products %}
<tr class="treegrid-{{ forloop.counter|add:1 }} treegrid-parent-{{ forloop.counter }}">
<td>{{ product.title }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
</table>
Answer the question
In order to leave comments, you need to log in
Everything is already in the documentation .
See the forloop.parentloop method
- For nested loops, this is the “outer” loop.
So?
<table class="table">
{% for category in categorys %}
{% if category.is_leaf_node %}
<tr class="treegrid-{{ forloop.counter }}">
{% set outer_counter = forloop.counter %}
<td>{{ category.name }}</td>
</tr>
{% for product in category.get_products %}
<tr class="treegrid-{{ forloop.counter|add:1 }} treegrid-parent-{{ outer_counter }}">
<td>{{ product.title }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
</table>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question