yet another digg style paginator

 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 6 years, 1 month ago
  2. Digg-style pagination, and retain GET params by hanson2010 1 year, 10 months ago
  3. Pretty Paginator Tag by sbaechler 2 years, 2 months ago
  4. Digg-like pagination by SmileyChris 4 years, 10 months ago
  5. Improved many-page pagination by dokterbob 3 years, 7 months ago

Comments

(Forgotten your password?)