Skip to content

Instantly share code, notes, and snippets.

@ferrouswheel
Last active December 19, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferrouswheel/5954448 to your computer and use it in GitHub Desktop.
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.
<!-- 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 }}">&laquo;</a></li>
{% else %}
<li class="disabled"><a href="?page={{ spreadsheets.previous_page_number }}">&laquo;</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 }}">&raquo;</a></li>
{% else %}
<li class="disabled"><a href="?page={{ spreadsheets.next_page_number }}">&raquo;</a></li>
{% endif %}
</ul>
</div>
# 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