paginator using url tag

 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# tag

from django import template

register = template.Library()

from django.template.defaulttags import url

class PaginatorNode(template.Node):

    def __init__(self, adjacent_pages=2, urltokens='', pagearg='', parser=None):
        self.adjacent_pages = adjacent_pages
        self.urltokens = urltokens
        self.pagearg = pagearg
        self.parser = parser

    def get_page_link(self, context, page):
        spaceorcomma = string.find(self.urltokens, ' ') != -1 and ',' or ' '
        c = 'url %s%s%s' % (self.urltokens, spaceorcomma, "%s=%s" % (self.pagearg, page))
        new_tokens = template.Token(template.TOKEN_TEXT, c)
        return {'number': page, 'link': url(self.parser, new_tokens).render(context)}

    def render(self, context):
        """
        To be used in conjunction with the object_list generic view.

        Adds pagination context variables for use in displaying first, adjacent and
        last page links in addition to those created by the object_list generic
        view.

        """
        page_numbers = [n for n in \
            range(context['page'] - self.adjacent_pages, context['page'] + self.adjacent_pages + 1) \
                if n > 0 and n <= context['pages']]

        from django.template.loader import get_template
        t = get_template('paginator.html')

        paginatorcontext = template.Context({
            'hits': context['hits'],
            'results_per_page': context['results_per_page'],
            'page': context['page'],
            'pages': context['pages'],
            'page_numbers': [self.get_page_link(context, n) for n in page_numbers],
            'next': self.get_page_link(context, context['next']),
            'previous': self.get_page_link(context, context['previous']),
            'has_next': context['has_next'],
            'has_previous': context['has_previous'],
            'show_first': 1 not in page_numbers,
            'first': self.get_page_link(context, 1),
            'show_last': context['pages'] not in page_numbers,
            'last': self.get_page_link(context, context['pages'])
        })
        return t.render(paginatorcontext)

def paginator(parser, token):
    bits = token.contents.split(' ')
    lim = len(bits) == 4 and 3 or 4
    urltokens = ' '.join(bits[2:lim])
    return PaginatorNode(adjacent_pages=int(bits[1]), urltokens=urltokens, pagearg=bits[lim], parser=parser)

register.tag(paginator)

# paginator.html

"""
{% spaceless %}
<span class="paginate-pages" title="Page Jump">{{ pages }} Pages</span>
{% if show_first %}<span class="paginate-first"><a href="{{ first.link }}" title="First Page">&laquo;</a></span>{% endif %}
{% if has_previous %}<span class="paginate-previous"><a href="{{ previous.link }}" title="Previous Page">&lt;</a></span>{% endif %}
{% for num in page_numbers %}
  {% ifequal num.number page %}
    <span class="paginate-current" title="Current Page">{{ num.number }}</span>
  {% else %}
    <span class="paginate-link"><a href="{{ num.link }}" title="Page {{ num.number }}">{{ num.number }}</a></span>
  {% endifequal %}
{% endfor %}
{% if has_next %}<span class="paginate-next"><a href="{{ next.link }}" title="Next Page">&gt;</a></span>{% endif %}
{% if show_last %}<span class="paginate-last"><a href="{{ last.link}}" title="Last Page">&raquo;</a></span>{% endif %}
{% endspaceless %}

"""

# urls.py

    ('^tags/(?P<tag>[^/]+)/$', 'tagging.views.tagged_object_list', {'model': Image, 'paginate_by':3}, 'image_tag'),
    ('^tags/(?P<tag>[^/]+)/(?P<page>\d+)/$', 'tagging.views.tagged_object_list', {'model': Image, 'paginate_by':3}, 'image_tag_paged'),

More like this

  1. Simple Paginate by laserlight 10 months, 1 week ago
  2. Django Uri Param Generator by ioan1k 4 years, 7 months ago
  3. Append paramaters to a GET querystring (template tag) by gregb 4 years, 9 months ago
  4. Paginator Tag by insin 7 years, 1 month ago
  5. Pagination Template Tag by ioan1k 4 years, 7 months ago

Comments

(Forgotten your password?)