Filter to adjust forloop.counter across pages in a paginated view

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django import template
from django.conf import settings

register = template.Library()

@register.filter
def adjust_for_pagination(value, page):
    value, page = int(value), int(page)
    adjusted_value = value + ((page - 1) * settings.RESULTS_PER_PAGE)
    return adjusted_value

# And the template snippet:
{% for object in object_list %}
<div class="serial-no">
    {% if is_paginated %}
        {{ forloop.counter|adjust_for_pagination:page }}
    {% else %}
       {{ forloop.counter }}
    {% endif %}
</div>
...
{% endfor %}

More like this

  1. Search results pagination by polarbear 7 years ago
  2. Basic CouchDB Paginator (Updated) by toke 5 years, 5 months ago
  3. Modelaware json serializer by fivethreeo 7 years, 1 month ago
  4. Paginator template tag using ObjectPaginator by Batiste 6 years, 11 months ago
  5. Tags & filters for rendering search results by exogen 6 years ago

Comments

dc (on March 26, 2009):

Can be achieved with:

{% for object in object_list %}
    {{ page_obj.start_index|add:forloop.counter0 }}
{% endfor %}

#

egmanoj (on March 29, 2009):

And the Truth shall set you free.

Thanks for pointing that out. I'll have fun removing that code from my repo ;)

#

(Forgotten your password?)