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.
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.
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.
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.
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>
You can use page_range
{% for page in objects.paginator.page_range %}
{% if forloop.counter != 1 %} | {% endif %}
{{ page }}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With