Login

Paginator Tag for 1.x

Author:
HM
Posted:
March 21, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

This is a Paginator Tag for 1.x. Since the context is less overfull, the template, paginator.html, needs more logic.

Put the tag in your templatetags and the template at the root of a template-directory.

The tag will work out of the box in a generic view, other views must provide is_paginated set to True, page_obj, and paginator. You can get the object_list from the page_obj: page_obj.object_list. See the pagination documentation.

 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
# The tag

from django import template

register = template.Library()

@register.inclusion_tag('paginator.html', takes_context=True)
def paginator(context):
    return {
            'page_obj': context['page_obj'],
            'paginator': context['paginator'],
            'is_paginated': context['is_paginated'],
            'object_list': context['object_list'],
            }

# An example paginator.html

{% if is_paginated %}
<div class="pagination">
    <span>
        {% ifnotequal page_obj.first page_obj.number %}
        <b><a href="?page={{ page_obj.first }}">|&lt;</a></b>
        {% endifnotequal %}
        {% if page_obj.has_previous %}
        <b><a href="?page={{ page_obj.previous_page_number }}">&lt;</a></b>
        {% endif %}
        {% for p in page_obj.paginator.pages %}
        {% ifequal p page_obj %}
        <b class="selected">{{ page_obj }}</b>
        {% else %}
        <b><a href="?page={{ p.number }}">{{ p }}</a></b>
        {% endifequal %}
        {% endfor %}
        {% if page_obj.has_next %}
        <b><a href="?page={{ page_obj.next_page_number }}">&gt;</a></b>
        {% endif %}
        {% ifnotequal page_obj.last page_obj.number %}
        <b><a href="?page={{ page_obj.last }}">&gt;|</a></b>
        {% endifnotequal %}
    </span>
</div>
{% endif %}

More like this

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

Comments

renatopedigoni (on March 25, 2009):

in order to run on 1.0 you must change this: {% for p in page_obj.paginator.pages %}

to {% for p in page_obj.paginator.page_range %}

#

Please login first before commenting.