Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add serial number in django list?

Tags:

django

I'm populating table using using for loop in django list view.

While populating I want to display serial number as well in the list.

{% for Attendee in filter.qs %} 
  {% if Attendee.checkin %}
    <tr>
      <td> Serial no. </td>
      <td>{{ Attendee.roll_no }}</td>
      <td>{{ Attendee.name }}</td>
      <td>{{ Attendee.branch }}</td>
    </tr>
    {% endif %}

As I'm using filter to display the results, it is not possible to maintain serial number in models.

I want something like this:

img

What are the other possible ways I can use to put serial numbers in list?

EDIT:

After using {{ forloop.counter }} in list, the serials aren't continuous. eg:

enter image description here

like image 615
alter123 Avatar asked Oct 24 '25 18:10

alter123


1 Answers

You can access current iteration of the loop via forloop.counter(1-indexed) or forloop.counter0(0-indexed). For your case:

{% for Attendee in filter.qs %} 
    {% if Attendee.checkin %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ Attendee.roll_no }}</td>
        <td>{{ Attendee.name }}</td>
        <td>{{ Attendee.branch }}</td>
    </tr>
{% endif %}
like image 185
Grigoriy Mikhalkin Avatar answered Oct 26 '25 08:10

Grigoriy Mikhalkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!