Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django paginator - how to show all page numbers available

I have simple issue:

I have this {{ objects.paginator.num_pages }} in template, which gives me the total number of pages that contain items.

now i want to show those page numbers like this

1 | 2 | 3 | 4 | 5

to achieve this, i need to make forloop till the num_pages. like for i to num_pages.

how is it possible in django template? i am reading some snippets, but they are a bit difficult to understand for me.

like image 612
doniyor Avatar asked Nov 03 '13 10:11

doniyor


People also ask

How does django deal with Pagination?

Note that you can give Paginator a list/tuple, a Django QuerySet , or any other object with a count() or __len__() method. When determining the number of objects contained in the passed object, Paginator will first try calling count() , then fallback to using len() if the passed object has no count() method.

Does Pagination improve performance django?

Additionally, switching to keyset pagination will improve the performance of page lookups and make them work in constant time. Django makes it easy to alter its default configuration, giving you the power to build a performant solution for pagination in Django.

What is paginator in django?

Django provides a few classes that help you manage paginated data – that is, data that's split across several pages, with “Previous/Next” links. These classes live in django/core/paginator.py. For examples, see the Pagination topic guide.


2 Answers

Formatted with twitter bootstrap and with links:

 <ul class="pagination nav navbar-nav">
    {% if objects.has_previous %}
            <li><a href="?page={{ objects.previous_page_number }}">Prev</a></li>
    {% endif %}
    {% for page in objects.paginator.page_range %}
        <li class="{% if objects.number == page  %}active{% endif %}"><a href="?page={{page }}">{{ page }}</a></li>
    {% endfor %}

    {% if objects.has_next %}
        <li> <a href="?page={{ objects.next_page_number }}">Next</a></li>
    {% endif %}
 </ul>
like image 54
rcc-e Avatar answered Oct 05 '22 23:10

rcc-e


You can use page_range

{% for page in objects.paginator.page_range %}
    {% if forloop.counter != 1 %} | {% endif %}
    {{ page }}
{% endfor %}
like image 29
falsetru Avatar answered Oct 05 '22 23:10

falsetru