Created
August 24, 2011 18:57
-
-
Save jefftriplett/1168864 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
""" | |
Author: Jeff Triplett | |
Based off: https://code.djangoproject.com/wiki/PaginatorTag | |
Thanks to Brent O'Connor for pointing it out. | |
""" | |
from django import template | |
register = template.Library() | |
def paginator(context, adjacent_pages=2): | |
""" | |
To be used in conjunction with the Django 1.3+ class based generic views. | |
Adds pagination context variables for use in displaying page_range, | |
Example usage:: | |
{% paginator %} | |
{% paginator 3 %} | |
""" | |
if 'is_paginated' in context and 'page_obj' in context and getattr(context['page_obj'], 'number', False): | |
page_number = context['page_obj'].number | |
num_pages = context['page_obj'].paginator.num_pages | |
page_range = [n for n in \ | |
range(page_number - adjacent_pages, page_number + adjacent_pages + 1) \ | |
if n > 0 and n <= num_pages] | |
context.update({ | |
'page_range': page_range, | |
'show_first': 1 not in page_range, | |
'show_last': num_pages not in page_range, | |
}) | |
register.inclusion_tag('includes/paginator.html', takes_context=True)(paginator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment