Login

Paginator template tag using ObjectPaginator

Author:
Batiste
Posted:
May 11, 2007
Language:
Python
Version:
.96
Score:
0 (after 2 ratings)

This template inclusion tag provide a way to have multiple pagination blocks in the same page. Aditionnal parameters in "request.GET" are also automaticaly keeped in pagination links.

Usage :

{% show_pagination users_paginator request "page_members" %}

The expected result :

[1] 2 3 … 14

Or :

1 … 5 6 [7] 8 9 … 14

 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
from django import template

register = template.Library()

@register.inclusion_tag('pagination.html')
def show_pagination(pagination, request, get_param_name):
    """
    pagination : ObjectPaginator from the view
    request : needed to keep get variables
    get_param_name : tell which parameters to use in links/request.GET

    All variables are set here for the template rendering.
    You will maybe not needs all for your own template.

    """
    if(request.GET.has_key(get_param_name)):
        current_page = int(request.GET[get_param_name])
    else:
        current_page = 0
    has_previous = pagination.has_previous_page(current_page)
    has_previous_previous = pagination.has_previous_page(current_page-1)
    has_next = pagination.has_next_page(current_page)
    has_next_next = pagination.has_next_page(current_page+1)
    need_pagination = has_previous or has_next
    
    has_start_page = current_page > 2
    has_end_page = (pagination.pages - current_page) > 3
    
    has_start_hellip = current_page > 3
    has_end_hellip = (pagination.pages - current_page) > 4
    
    total_pages = pagination.pages
    
    get_params = ''
    for key, value in request.GET.iteritems():
        if not key == get_param_name:
            if get_params == '':
                get_params = '?'+key+'='+str(value[0])
            else:
                get_params += '&'+key+'='+str(value[0])
    if get_params == '':
        get_params = '?'+get_param_name+'='
    else:
        get_params += '&'+get_param_name+'='
    return locals()

'''An example for pagination.html file :'''

{% load math_filters %}
{% if need_pagination %}
{% if has_start_page %}<a href="{{ get_params }}0">1</a>{% endif %}
{% if has_start_hellip %}&hellip;{% endif %}
{% if has_previous_previous %}<a href="{{ get_params }}{{ current_page|sub:2 }}">{{ current_page|sub:1 }}</a>{% endif %}
{% if has_previous %}<a href="{{ get_params }}{{ current_page|sub:1 }}">{{ current_page }}</a> {% endif %}
<strong>[{{ current_page|add:1 }}]</strong>
{% if has_next %}<a href="{{ get_params }}{{ current_page|add:1 }}"> {{ current_page|add:2 }}</a>{% endif %}
{% if has_next_next %}<a href="{{ get_params }}{{ current_page|add:2 }}"> {{ current_page|add:3 }}</a>{% endif %}
{% if has_end_hellip %}&hellip;{% endif %}
{% if has_end_page %}<a href="{{ get_params }}{{ total_pages|sub:1 }}">{{ total_pages }}</a>{% endif %}
{% endif %}

'''Note that this template use this stupid sub filter :'''

from django import template

register = template.Library()

def sub(value, arg=None):
    return value-arg

register.filter('sub', sub)

'''An example for the view :'''

from django.core.paginator import ObjectPaginator, InvalidPage
if request.GET.has_key('page_members'):
    page_users = int(request.GET['page_members'])
else:
    page_users = 0
users = User.objects.order_by('-last_login')
users_paginator = ObjectPaginator(users, 5)
users = users_paginator.get_page(page_users)

'''Code example of the template tag :'''

{% load pagination_filters %}
{% show_pagination users_paginator request "page_members" %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 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, 7 months ago

Comments

priyesh (on August 8, 2007):

Could you please add a bit about the specific location of what files and pieces of code should go where? in particular I am confused about the sub method, and also about what variable need to be passed from views.py

#

priyesh (on August 10, 2007):

{% load math_filters %} Is that a custom filter that you use?

Also if ou could elaborate more on the naming conventions of your files and arguments, that would be very useful.

#

Please login first before commenting.