Skip to content

Instantly share code, notes, and snippets.

@ndarville
Created September 2, 2012 15:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndarville/3600717 to your computer and use it in GitHub Desktop.
Save ndarville/3600717 to your computer and use it in GitHub Desktop.
Django Pagination Code
{% if page.has_other_pages %}
<div class="pagination">
<!-- Older page -->
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}" title="Go to the older threads.">&lt;&nbsp;Older</a>
{% endif %}
<!-- First page -->
{% if current_page > 4 %}
<a href="?page=1">1</a>&nbsp;&#8943;
{% endif %}
<!-- Current page -3 -->
{% if current_page > 3 %}
<a href="?page={{ current_page|add:"-3" }}">{{ current_page|add:"-3" }}</a>
{% endif %}
<!-- Current page -2 -->
{% if current_page > 2 %}
<a href="?page={{ current_page|add:"-2" }}">{{ current_page|add:"-2" }}</a>
{% endif %}
<!-- Current page -1 -->
<!-- current_page > 1 -->
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">{{ current_page|add:"-1" }}</a>
{% endif %}
<!-- Current page -->
{{ current_page }}
<!-- Current page +1 -->
<!-- current_page < total_pages -->
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">{{ current_page|add:"1" }}</a>
{% endif %}
<!-- Current page +2 -->
{% if current_page < total_pages|add:"-1" and total_pages > 2 %}
<a href="?page={{ current_page|add:"2" }}">{{ current_page|add:"2" }}</a>
{% endif %}
<!-- Current page +3 -->
{% if current_page < total_pages|add:"-2" %}
<a href="?page={{ current_page|add:"3" }}">{{ current_page|add:"3" }}</a>
{% endif %}
<!-- Last page -->
{% if current_page < total_pages|add:"-3" and total_pages > 4 %}
&#8943;&nbsp;<a href="?page={{ total_pages }}">{{ total_pages }}</a>
{% endif %}
<!-- Newer page -->
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}" title="Go to the newer threads.">Newer&nbsp;&gt;</a>
{% endif %}
</div>
{% endif %}
@ndarville
Copy link
Author

To see a working example, check out my pony-forum repo.

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