Last active
December 19, 2015 12:19
-
-
Save ferrouswheel/5954448 to your computer and use it in GitHub Desktop.
Want to use Django pagination and bootstrap's paginator div? Want to customise and avoid installing an entirely new app? Just copy, paste and customise the below.
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
<!-- Assume spreadsheets is a django pagination object and pages is an iterable of page numbers to display --> | |
<!-- Note: this doesn't preserve GET parameters or hash fragments --> | |
<div class="pagination"> | |
<ul> | |
{% if spreadsheets.has_previous %} | |
<li><a href="?page={{ spreadsheets.previous_page_number }}">«</a></li> | |
{% else %} | |
<li class="disabled"><a href="?page={{ spreadsheets.previous_page_number }}">«</a></li> | |
{% endif %} | |
{% for page in pages %} | |
{% if page %} | |
{% ifequal page spreadsheets.number %} | |
<li class="active"><a href="#">{{ page }}</a></li> | |
{% else %} | |
<li><a href="?page={{ page }}">{{ page }}</a></li> | |
{% endifequal %} | |
{% else %} | |
<li class="disabled"><a href="#">...</a></li> | |
{% endif %} | |
{% endfor %} | |
{% if spreadsheets.has_next %} | |
<li><a href="?page={{ spreadsheets.next_page_number }}">»</a></li> | |
{% else %} | |
<li class="disabled"><a href="?page={{ spreadsheets.next_page_number }}">»</a></li> | |
{% endif %} | |
</ul> | |
</div> |
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
# To create the pages context variable... | |
# | |
# Note: you should check page is an integer. | |
page = request.GET.get('page') | |
lower = max(int(page)-3, 1) | |
upper = min([int(page)+3, paginator.num_pages]) | |
pages = range(lower, upper + 1) | |
if upper < paginator.num_pages: | |
pages.append(None) | |
# See django documention on how to create the paginator object (called spreadsheets in the attached template). | |
# https://docs.djangoproject.com/en/dev/topics/pagination/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment