Getting total count of the iteration in multiple for loops: django template

I am working on a project using django I have 2 lists classes and students I need to iterate in these and print the s.no , class.name and class.student.name using django template

the code snippets looks something like:

            {% for class in classes %}
            {% for student in class.students %}
            <tr>
                <td>#{{ count }}</td>
                <td>{{ student.name }}</td>
                <td>{{ class.name }}</td>
            </tr>
            {% endfor %}
            {% endfor %}

I am not able to get the correct count as we have either the parentloop counter of for loop counter but i need the count of total iteration

i tried something like

            {% with count=0 %}
            {% for class in classes %}
            {% for student in class.students %}
            {% with count=count|add:1 %}
            <tr>
                <td>#{{ count }}</td>
                <td>{{ student.name }}</td>
                <td>{{ class.name }}</td>
            </tr>
            {% endwith %}
            {% endwith %}
            {% endfor %}
            {% endfor %}

but this always give the count as 1

changing the position of the for loop also did not worked. Do we have a solution to this?

Regards

Back to Top