How to show the correct object numbers when using django-pagination

I use django-pagination to split the list of objects. It works flawlessly. I want to specify a number for each object on the page, and for this I use {{forloop.counter}} , but the problem is that it starts counting the object with 1 on each page. I wanted to show the actual number of objects.

Let's say if I am breaking the pages into 10 objects per page, then I want the number of objects to be 11 for the first object on page 2. I tried to write a template filter for this, but somehow I can not send both request.get.page and {{forloop.counter}} to my filter function. I can not do it.

Any help for referrals would be appreciated.

+8
django django-templates
source share
3 answers

You can use the add tag to add the current counter from the index page to the plus.

 {{ forloop.counter|add:paginator.page.start_index }} 
+14
source share

I used this in my template and it works correctly

 {{ page_obj.start_index|add:forloop.counter0 }} 
+2
source share

add paginator s start index for loop counter starting at zero

in the template

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

in sight

 objects = Abcdef.objects.all() # Abcdef is the modal paginator = Paginator(objects, 10) page_number = request.GET.get('page') try: page_objects = paginator.page(page_number) except PageNotAnInteger: page_objects = paginator.page(1) except EmptyPage: page_objects = paginator.page(paginator.num_pages) data = { "page_objects" : page_objects, } return render(request, "template/template.html", data) 
+1
source share

All Articles