Last active
August 29, 2015 14:01
-
-
Save epicserve/da02134fe7ea1ceb5618 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{% if is_paginated %} | |
{% load i18n %} | |
<ul class="pagination"> | |
<li{% if not page_obj.has_previous %} class="disabled"{% endif %}><a href="{% if not page_obj.has_previous %}#{% else %}?page={{ page_obj.previous_page_number }}{{ getvars }}{% endif %}">«</a></li> | |
{% if show_first %} | |
<li><a href="?page=1{{ getvars }}">1</a></li> | |
<li><span>...</span></li> | |
{% endif %} | |
{% for linkpage in page_numbers %} | |
<li{% ifequal linkpage page %} class="active"{% endifequal %}><a href="?page={{ linkpage }}{{ getvars }}">{{ linkpage }}</a></li> | |
{% endfor %} | |
{% if show_last %} | |
<li><span>...</span></li> | |
<li><a href="?page={{ num_pages }}{{ getvars }}">{{ num_pages }}</a></li> | |
{% endif %} | |
<li{% if not page_obj.has_next %} class="disabled"{% endif %}><a href="{% if not page_obj.has_next %}#{% else %}?page={{ page_obj.next_page_number }}{{ getvars }}{% endif %}">»</a></li> | |
</ul> | |
{% endif %} |
This file contains hidden or 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
from django import template | |
register = template.Library() | |
def paginator(context, adjacent_pages=2): | |
""" | |
A template tag that can be used on any view that uses Django's Pagination | |
class (i.e. django.views.generic.ListView) to create pagination links with | |
page numbers. | |
Usage:: | |
{% load paginator %} | |
{% paginator %} | |
or | |
{% paginator 3 %} | |
""" | |
page_obj = context['page_obj'] | |
page = page_obj.number | |
num_pages = page_obj.paginator.num_pages | |
start_page = max(page - adjacent_pages, 1) | |
if start_page <= 3: | |
start_page = 1 | |
end_page = page + adjacent_pages + 1 | |
if end_page >= num_pages - 1: | |
end_page = num_pages + 1 | |
page_numbers = [n for n in range(start_page, end_page) if 0 < n <= num_pages] | |
page_obj = context['page_obj'] | |
return { | |
'page_obj': page_obj, | |
'is_paginated': page_obj.paginator.num_pages > 1, | |
'num_pages': num_pages, | |
'page': page, | |
'page_numbers': page_numbers, | |
'getvars': "&%s" % "&".join(["%s=%s" % (k, v) for k, v in context['request'].GET.items() if k != 'page']), | |
'show_first': 1 not in page_numbers, | |
'show_last': num_pages not in page_numbers, | |
} | |
register.inclusion_tag('blocks/pagination.html', takes_context=True)(paginator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment