-2

Is there an option to see records index on the current page and total?

For example:

Records 51-100, 980 total

on page #2

I could maybe use a {% with object_list.count as "pages_total" %} templatetag then lots of code.. and {% endwith %} at the end code.. - done before the {% paginate %} or {% autopaginate %} but this doesn't seem to be a nice solution. Also it makes another db call.

I can't find this in documentation, and {{ object_list.count }} gives 50 in this situation.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Robert
  • 365
  • 8
  • 23

3 Answers3

4

If you are using Django Endless Pagination 2.0, you will able to use this in your template:

{% load endless %}
{% paginate entries %}
{% for entry in entries %}
    {# your code to show the entry #}
{% endfor %}
{% get_pages %}
Showing entries 
{{ pages.current_start_index }}-{{ pages.current_end_index }} of
{{ pages.total_count }}.
{{ pages }}

All this is in Django-endless-pagination document

1

People tend to overcomplicate this. I found thee current_start_index and current_end_index functions fairly well documented in the source ( but not in the actual documentation ).

Here is how you would use them to achieve that.

{{ pages.current_start_index }} through {{ pages.current_end_index }} of {{ pages.total_count }} results

Hope this helps someone :D

user3608721
  • 31
  • 1
  • 2
0

All django-pagination paginators extends Django's built-in Paginator class. The best solution is to use Paginator.count to get the total number of elements on all pages (if you're passing QuerySet object to the Paginator). According to the doc:

When determining the number of objects contained in object_list, Paginator will first try calling object_list.count(). If object_list has no count() method, then Paginator will fallback to using len(object_list). This allows objects, such as Django's QuerySet, to use a more efficient count() method when available.

So yes, it will do another DB query, but it's unavoidable if you do not know the number of elements. Django will use count() which uses SELECT COUNT(*) statement, which is the best way to get number of rows.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • That will make including pagination div harder. Using `{% include "paginator.html" %}`. Both solutions I mentioned are aware of total objects count.. they must have it because they split queryset to parts. I am just curious why there is no `{% pages.total_count %}` template tag, or variable available -or maybe I'm missing something dome docs here. – Robert Feb 24 '12 at 13:18
  • Why it'll make including pagination div harder? – Mariusz Jamro Feb 24 '12 at 13:42
  • You have to keep a `{{ total_count }}` from `{% with object_list.count as "total_count" %}`. Only then You can use paginator div (which shows total count of records). I think that If either django-pagination or django-endless-pagination provided such variable this would be easier. The code would be cleaner, and one less query would hit the db. Simply loading pagination_tags and then autopaginating object list + including paginator div (that's how I actually do it now) would be better - I don't have total count & current page start/end index. – Robert Feb 24 '12 at 14:05
  • Can you provide 'paginator.html' code? I believie you can add the 'total_count' to the context in [pagination_tags.py](http://code.google.com/p/django-pagination/source/browse/trunk/pagination/templatetags/pagination_tags.py). It's like one line of code. – Mariusz Jamro Feb 24 '12 at 14:19
  • Currently I am using django-endless-pagination, and doing a new gmail like controls next/previous with ` ` As for patching, yeah.. but my question could be "why so obvious total count var is missing (maybe it's not...)". – Robert Feb 24 '12 at 19:51