Login

yet another digg style paginator

Author:
akonsu
Posted:
October 13, 2009
Language:
Python
Version:
1.4
Tags:
paginator digg
Score:
1 (after 1 ratings)

put this code into your application's __init__.py

it adds a mixin to the Paginator class that implements a digg style pagination. the mixin has just one method called digg_page_range that takes the current page object as the parameter. this method is an iterator which yields page numbers with None values representing gaps. this iterator is similar to the original paginator's method page_range and it can be used in your code to emit the needed markup.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.core.paginator import Paginator

class PaginatorMixin :
    def digg_page_range(self, page) :
        BODY_PAGES = 9
        MARGIN_PAGES = 3
        TAIL_PAGES = 2

        position = 1

        p, q = TAIL_PAGES, max(1, min(page.number - BODY_PAGES / 2, self.num_pages - BODY_PAGES + 1))

        if q - p > MARGIN_PAGES :
            for x in xrange(position, p + 1) :
                yield x

            yield None

            position = q

        p, q = q + BODY_PAGES - 1, self.num_pages - TAIL_PAGES + 1

        if q - p > MARGIN_PAGES :
            for x in xrange(position, p + 1) :
                yield x

            yield None

            position = q

        for x in xrange(position, self.num_pages + 1) :
            yield x

if PaginatorMixin not in Paginator.__bases__ :
    Paginator.__bases__ = (PaginatorMixin,) + Paginator.__bases__

#
# example usage in a custom tag (used from within django.views.generic.list_detail.object_list template):
#
# @register.inclusion_tag('paginator.html', takes_context = True)
# def pages(context) :
#     page_obj = context['page_obj']
#     return {'digg_page_range' : page_obj.paginator.digg_page_range(page_obj), 'page_obj' : page_obj}
#
# where paginator.html might look like this:
#
#<span class="pages">
#  {% if page_obj.has_previous %}<a href="?page={{page_obj.previous_page_number}}">&lt;</a>{% endif %}
#  {% for p in digg_page_range %}
#    {% if not p %}...{% else %}{% ifequal p page_obj.number %}<span>{{p}}</span>{% else %}<a href="?page={{p}}">{{p}}</a>{% endifequal %}{% endif %}
#  {% endfor %}
#  {% if page_obj.has_next %}<a href="?page={{page_obj.next_page_number}}">&gt;</a>{% endif %}
#</span>

More like this

  1. Stateful paginator, digg style by miracle2k 7 years, 2 months ago
  2. Digg-style pagination, and retain GET params by hanson2010 2 years, 11 months ago
  3. Pretty Paginator Tag by sbaechler 3 years, 3 months ago
  4. Digg-like pagination by SmileyChris 6 years ago
  5. Improved many-page pagination by dokterbob 4 years, 8 months ago

Comments

Please login first before commenting.