My template code is this
{% for announcement in announcements %}
<tr>
<td>{{ count }}</td>
<td>{{ announcement.title }}</td>
<td>{{ announcement.user.profile.name }}</td>
<td>{{ announcement.modified }}</td>
</tr>
{% endfor %}
I want to count down from the length of queryset
to 1.
How can I do that?
There is a reverse counter called forloop.revcounter
which counts from the length of the queryset to 1.
{% for announcement in announcements %}
<tr>
<td>{{ forloop.revcounter }}</td>
<td>{{ announcement.title }}</td>
<td>{{ announcement.user.profile.name }}</td>
<td>{{ announcement.modified }}</td>
</tr>
{% endfor %}
There are both zero (forloop.revcounter0
) and 1 indexed (forloop.revcounter
) reverse counting available.
Django template "for" documentation.
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