Created
August 28, 2017 00:55
-
-
Save jmn/e6cb6cfdff087e024aa08ca0c633bd2e to your computer and use it in GitHub Desktop.
Django pagination controls for Bootstrap 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if is_paginated %} | |
<nav aria-label="Page navigation example"> | |
<ul class="pagination justify-content-left"> | |
{% if page_obj.has_previous %} | |
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">«</a></li> | |
{% else %} | |
<li class="page-item disabled"><a class="page-link" href="#"><span>«</span></a></li> | |
{% endif %} | |
{% for i in paginator.page_range %} | |
{% if page_obj.number == i %} | |
<li class="page-item active"><a class="page-link" href="#">{{ i }} <span class="sr-only">(current)</span></a></li> | |
{% else %} | |
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
{% if page_obj.has_next %} | |
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">»</a></li> | |
{% else %} | |
<li class="page-item disabled"><a class="page-link" href="#"><span>»</span></a></li> | |
{% endif %} | |
</ul> | |
</nav> | |
{% endif %} |
If there are 100 pages, do we see links for all 1-100 items?
How do we show prev, few pages links and next?
Hi there! At this point you will see all the 100 pages. You can mix and match some more code, like here:
https://stackoverflow.com/questions/41131802/django-paginator-page-range-for-not-displaying-all-numbers
{% for i in items.paginator.page_range %}
{% if l <= i.number|add:5 and l >= i.number|add:-5 %}
<li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
{% endif %}
{% endfor %}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!
here goes a little improvement as you can call page_obj.paginator.page_range, and easy the usage with {% with %}