Login

better paginator template tag

Author:
amitu
Posted:
October 14, 2008
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

This is slight improvement over Paginator|Snippet 73. That used to not work properly if querystring already contains other parameters, like search result page.

website/paginator.html:

<br /><center>
<span class="lbottom">
{% if has_previous %}<a href="{{ path }}page={{ previous }}"><< Previous </a>{% else %}<span>Previous </span>{% endif %}
{% if show_first %}<a href="{{ path }}page=1">First </a>{% endif %}
{% for page_no in page_numbers %}
    {% ifnotequal page_no page %}
        <a href="{{ path }}page={{ page_no }}">{{ page_no }} </a>
    {% else %}
    {{ page_no }} 
    {% endifnotequal %}
{% endfor %}
{% if show_last %}<a href="{{ path }}page={{ pages }}">Last </a>{% endif %}
{% if has_next %}<a href="{{ path }}page={{ next }}">Next >></a>{% else %}<span>Next </span>{% endif %}
</span>
<br /></center>
 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
# original: http://www.djangosnippets.org/snippets/73/
from django import template
import cgi, urllib

register = template.Library()

@register.inclusion_tag('website/paginator.html', takes_context=True)
def paginator(context, adjacent_pages=2):
    """
    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'] - adjacent_pages, 
            context['page'] + adjacent_pages + 1
        ) 
        if n > 0 and n <= context['pages']
    ]
    path = context.get("paginator_path_override", context["request"].path)
    querystring = context["request"].META.get("QUERY_STRING")
    if not querystring: querystring = ""
    query_dict = dict(cgi.parse_qsl(querystring))
    if "page" in query_dict: del query_dict["page"]
    querystring = urllib.urlencode(query_dict)
    if querystring:
        path = "%s?%s&" % ( path, querystring )
    else: 
        path = "%s?" % path
        
    return {
        'hits': context['hits'],
        'results_per_page': context['results_per_page'],
        'page': context['page'],
        'pages': context['pages'],
        'page_numbers': page_numbers,
        'next': context['next'],
        'previous': context['previous'],
        'has_next': context['has_next'],
        'has_previous': context['has_previous'],
        'show_first': 1 not in page_numbers,
        'show_last': context['pages'] not in page_numbers,
        'path': path,
    }

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.