Paginator

Drop-in replacement for Django’s django.core.paginator with additional goodness

Django’s paginator class has a page_range method returning a list of all available pages. If you got lots and lots of pages this is not very helpful. Towel’s page class (not paginator class!) sports a page_range method too which only returns a few pages at the beginning and at the end of the page range and a few pages around the current page.

All you have to do to use this module is replacing all imports from django.core.paginator with towel.paginator. All important classes and all exceptions are available inside this module too.

The page range parameters can be customized by adding a PAGINATION setting. The defaults are as follows:

PAGINATION = {
    'START': 6, # pages at the beginning of the range
    'END': 6, # pages at the end of the range
    'AROUND': 5, # pages around the current page
    }
exception towel.paginator.InvalidPage
exception towel.paginator.PageNotAnInteger
exception towel.paginator.EmptyPage
class towel.paginator.Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)

Custom paginator returning a Page object with an additional page_range method which can be used to implement Digg-style pagination

page(number)

Returns a Page object for the given 1-based page number.

class towel.paginator.Page(page)

Page object for Digg-style pagination

page_range

Generates a list for displaying Digg-style pagination

The page numbers which are left out are indicated with a None value. Please note that Django’s paginator own page_range method isn’t overwritten – Django’s page_range is a method of the Paginator class, not the Page class.

Usage:

{% for p in page.page_range %}
    {% if p == page.number %}
        {{ p }} <!-- current page -->
    {% else %}
        {% if p is None %}
            &hellip;
        {% else %}
            <a href="?page={{ p }}">{{ p }}</a>
        {% endif %}
    {% endif %}
{% endfor %}