Cipher
Cipher

Reputation: 2122

For loop Count in django template

I want to display a number of the count in Django template in sequence.

Like if for loop iterate 5 time then it should print 0,1,2,3,4 in Django template.

users contains string which I pass from views.py

{% for user in users %}
    <tr>
        <td>
            FOR LOOP ITERATE COUNT
        </td>
        <td>
            {{ user.first_name }}
        </td>
    </tr>
{% endfor %}

Upvotes: 1

Views: 5131

Answers (1)

Rarblack
Rarblack

Reputation: 4664

You can user {{forloop.counter0}}:

{% for user in users %}
    <tr>
        <td>
            {{forloop.counter0}}
        </td>
        <td>
            {{ user.first_name }}
        </td>
    </tr>
{% endfor %}

Upvotes: 8

Related Questions