Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
How can I reverse count a queryset in a for loop?

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?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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 %}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!