Skip to content

Instantly share code, notes, and snippets.

@raelmax
Created July 18, 2011 17:22
Show Gist options
  • Save raelmax/1090095 to your computer and use it in GitHub Desktop.
Save raelmax/1090095 to your computer and use it in GitHub Desktop.
With this codesnippet your pagination don't show all page links, only two next pages and two previous pages, don't creating monstrous list of pages! :)
{% if is_paginated %}
<nav id="pagination">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous page</a>
{% endif %}
{% for page in page_obj.paginator.page_range %}
{% if page >= page_obj.number|add:"-2" and page < page_obj.number %}
<a href="?page={{ page }}">{{ page }}</a>
{% endif %}
{% ifequal page_obj.number page %}
<span>{{ page_obj.number }}</span>
{% endifequal %}
{% if page <= page_obj.number|add:"2" and page > page_obj.number %}
<a href="?page={{ page }}">{{ page }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next page</a>
{% endif %}
</nav>
{% endif %}
@raelmax
Copy link
Author

raelmax commented Jul 18, 2011

This is the django template language. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment