Reputation: 37876
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.
Upvotes: 8
Views: 8154
Reputation: 321
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>
Upvotes: 22
Reputation: 369074
You can use page_range
{% for page in objects.paginator.page_range %}
{% if forloop.counter != 1 %} | {% endif %}
{{ page }}
{% endfor %}
Upvotes: 11