5 Steps To Pagination in Django Web Application

by Ather Rashid

Django provides a way of representing our applications in a more dynamic form giving an interactive feel to web app. The same web app becomes slow when we have more data to load to our web page. Django provides a way to send specific amount of data to web page to load it faster, to put it simply, paginate the data according to our need. This can be implemented by using django-bootstrap-pagination. For this we need to install some pre-requisites.

1) The first step would be to install the requirements starting with django-bootstrap-pagination.

To install it using pip:

pip install django-bootstrap-pagination

OR

For Download:

http://pypi.python.org/pypi/django-bootstrap-pagination

2) Once the django-bootstrap-pagination is installed we need to add it to the list of installed apps in our settings.py file:

INSTALLED_APPS = (
'bootstrap_pagination',
)

3) Next, we need to implement this in our web app and for this we need to look into a no. of terms that will be used. To start with the terms we can first describe paginator.py file which is present in django core packages as all the classes that are to be used in pagination are located in this file.

4) Now starting with Paginator class its constructor is of the format:

class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)

In Paginator class there are two required arguments one is object_list (data in the form of list, tuple or django queryset with a count or a __len__ method) and the other is per_page (deciding the no. of items to be allowed on the page). Regarding the orphans (minimum no. of items to be allowed on last page with default value of zero) and allow_empty_first_page (whether or not the first page is allowed to be empty. If False and object_list is empty, then an EmptyPage error will be raised). They are optional arguments.

Now let’s suppose we have a user profile model in models.py file with structure as below:

from django.db import models
from django.contrib.auth.models import User

Class UserProfile(models.Model):
    user = models.OneToOneField(User,verbose_name='User', related_name='profile')
    address = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_now_add=True)
    modified_on = models.DateTimeField(auto_now=True)
    login_count = models.PositiveIntegerField(default=0)

Now in our views.py we can proceed as:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from myapp.models import UserProfile

def UserList(request):
    users = UserProfile.objects.all()
    paginator = Paginator(user_data, 10) # Shows only 10 records per page

    page = request.GET.get('page')
    try:
        users = paginator.page(page)
    except PageNotAnInteger:
    # If page is not an integer, deliver first page.
        users = paginator.page(1)
    except EmptyPage:
    # If page is out of range (e.g. 7777), deliver last page of results.
        users = paginator.page(paginator.num_pages)

    return render_to_response('users.html', {"users": users})

5) Once we create the views we can proceed to HTML part to create users.html page:

/*users.html*/
{% for user in users %}
<!-- where each user is an objects of UserProfile model -->
{{ user.address|capfirst }}<br />

{% endfor %}

<div class="pagination">
<span class="step-links">
{% if users.has_previous %}
<a href="?page={{ users.previous_page_number }}">previous</a>
{% endif %}

<!-- Showing the page numbers in Html Page -->
<span class="current">
Page {{ users.number }} of {{ users.paginator.num_pages }}.
</span>

{% if users.has_next %}
<a href="?page={{ users.next_page_number }}">next</a>
{% endif %}
</span>
</div>

In HTML part, the attributes such as paginator.num_pages gives total no. of pages, has_previous() checks if there is a previous page and has_next() checks if there is a next page and number gives page-number for the page.

Follow the above steps to implement pagination to your html pages to display limited amount of data for web application to deliver better performance and smooth process.

To learn more about Web

Contact Us

One thought on “5 Steps To Pagination in Django Web Application”

Leave a Reply

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

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts