Example how to use django-pagination in Django 1.3


Django-pagination is great module which ease for creating pagination pages. Usually, we make query to get data from database. Then on templates, we iterating the variable that contains data. Because large of data, we want it get paginate. Using default Django pagination will take several codes, eg :

views.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, InvalidPage, EmptyPage

def index(request):
    plan = Plan.objects.all()
    paginator = Paginator(plan, 5)
   
    try:
        page = int(request.GET.get(‘page’, ‘1’))
    except ValueError:
        page = 1
   
    try:
        fp = paginator.page(page)
    except (EmptyPage, InvalidPage):
        fp = paginator.page(paginator.num_pages)

    return render_to_response(‘myapp/index.html’, {‘mydata’: fp})


index.html:

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
{% for data in mydata.object_list %}

   <td>{{ data.id }}</td>
   <td>{{ data.uploader }}</td>                
   <td>{{ data.created }}</td>
   <td>{{ data.filename }}</td>
</tr>
{% endfor %}
   
<span>
{% if mydata.has_previous %}
    <a href="?page={{ insurance_plan.previous_page_number }}">previous</a>
{% endif %}
   
{% for num in insurance_plan.paginator.page_range %}
    {% ifequal num insurance_plan.number %}
        <span class="current page">{{ num }}</span>
    {% else %}
        <a href="?page={{ num }}"> {{ num }}</a>
    {% endifequal %}
{% endfor %}
   
{% if insurance_plan.has_next %}
    <a href="?page={{ insurance_plan.next_page_number }}"> Next </a>
{% endif %}
</span>

We can make this more simple by using “django-pagination” module. First, download and install it by :

1
sudo pip install django-pagination

Or take it from github by :

1
git clone https://github.com/ericflo/django-pagination.git

Then, we should edit our settings.py by adding “request” on TEMPLATE_CONTEXT_PROCESSORS and “pagination” on INSTALLED_APPS :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request"
)

INSTALLED_APPS = (
    …
    ‘pagination’,
)

Then let see the difference :

views.py

1
2
3
4
5
6
7
from django.shortcuts import render_to_response
from django.template.context import RequestContext

def index(request):
    plan = Plan.objects.all()

    return render_to_response(‘myapp/index.html’, {‘mydata’: fp}, context_instance=RequestContext(request))

index.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
{% load pagination_tags %}

{% block content %}
  {% autopaginate data 10 %}
  {% for data in mydata %}
     <td>{{ data.id }}</td>
     <td>{{ data.uploader }}</td>                  
     <td>{{ data.created }}</td>
     <td>{{ data.filename }}</td>
  </tr>
  {% endfor %}
  {% paginate %}
{% endblock %}

Remember, put block load pagination_tags “{% load pagination_tags %}” on top of your template.

FYI,

1
{% autopaginate data 10 %}

Is meaning paginate data for 10 results in one page.

Django-paginate will handle all of pagination which we don’t worried about
link in pagination results. Easy right ? 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.