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.
If you are looking for a counter which starts from 1 and increments while looping, you should use django's forloop.counter
{% for announcement in announcements %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ announcement.title }}</td>
<td>{{ announcement.user.profile.name }}</td>
<td>{{ announcement.modified }}</td>
</tr>
{% endfor %}