Skip to content

Instantly share code, notes, and snippets.

@ceolson01
Last active April 17, 2016 16:03
Show Gist options
  • Save ceolson01/271f6ff5db35a90ef31e3ad1aefd8e13 to your computer and use it in GitHub Desktop.
Save ceolson01/271f6ff5db35a90ef31e3ad1aefd8e13 to your computer and use it in GitHub Desktop.
Django & Bootstrap Pagination for Large Number of Pages
{% if listings.has_previous or listings.has_next %}
<div class="text-center">
<ul class="pagination pagination-sm">
{% if listings.has_previous %}
<li><a href="?page=1">1</a></li>
<li><a href="?page={{ listings.previous_page_number }}">‹</a></li>
{% endif %}
{% for i in paginator.page_range %}
{% if i < listings.number and i >= listings.number|add:"-4" and i != listings.number and i != 1 %}
<li><a href="?page={{i}}">{{i}}</a></li>
{% elif i > listings.number and i <= listings.number|add:"4" and i != listings.number and i != listings.paginator.num_pages %}
<li><a href="?page={{i}}">{{i}}</a></li>
{% elif listings.number == i %}
<li class="active"><a href="?page={{i}}">{{i}}</a></li>
{% endif %}
{% endfor %}
{% if listings.has_next %}
<li><a href="?page={{ listings.next_page_number }}">›</a></li>
<li><a href="?page={{ listings.paginator.num_pages }}">{{ listings.paginator.num_pages }}</a></li>
{% endif %}
</ul>
</div>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment