[Solved]-Django template tags {{for/empty}} for loop variable

16👍

This is an expected behavior. Simplifying we have:

{% for A ... %}
    {{ forloop.* }} is there for the 'for A ...'

    {% for B ... %}
        {{ forloop.* }} is there for the 'for B ...'
        {{ forloop.parentloop.* }} refers to the 'for A ...'
    {% empty %}
        {{ forloop.* }} is there for the 'for A ...' !!!
    {% endfor %}
{% endfor %}

In {% empty %}, {{ forloop }} refers to the parent forloop! Change:

var js_variable{{ forloop.parentloop.counter0 }} = []

With:

var js_variable{{ forloop.counter0 }} = []
👤jpic

Leave a comment