Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Created August 24, 2011 18:57
Show Gist options
  • Save jefftriplett/1168864 to your computer and use it in GitHub Desktop.
Save jefftriplett/1168864 to your computer and use it in GitHub Desktop.
"""
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