How to add Pagination in Django Project?
Last Updated :
07 Aug, 2023
Pagination system is one of the most common features in blogs, search engine , list of result etc. Seeing the popularity of pagination system django developers have build a Paginator class so that web developers do not have to think of the logic to make paginators.
Paginator Class live in django/core/paginator.py . So to use Paginator Class we first need to import it from django.core.paginator
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Syntax :
p = Paginator(list_of_objects, no_of_objects_per_page)
The first argument is the list of objects which will be distributed over pages. The second argument denotes the number of objects that will be displayed on each page. These two arguments are required.
However Paginator Class takes two more optional arguments which are listed below –
- orphans – its value must be an integer less than the no_of_objects_per_page value. It tells if the last page has minimum number of objects. If the number of remaining objects in the last page is less than or equal to the value of this argument then those objects will be added to the previous page. Default value is 0.
- allow_empty_first_page – It takes Boolean values. Whether or not the first page is allowed to be empty.
Note : the first argument need not to be a list. Instead it can be a tuple, queryset or other sliceable object with a count() or __len__() method.
How to use Paginator Class?
Suppose we are developing a blogging website. We have defined Post model in models.py and we have creates 8 such posts. Now in views.py, we have written the following code –
Python3
from django.shortcuts import render
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
posts = Post.objects. all ()
p = Paginator(posts, 5 )
page_number = request.GET.get( 'page' )
try :
page_obj = p.get_page(page_number)
except PageNotAnInteger:
page_obj = p.page( 1 )
except EmptyPage:
page_obj = p.page(p.num_pages)
context = { 'page_obj' : page_obj}
return render(request, 'index.html' , context)
|
At the third line , Paginator Class is imported. In the index function we have constructed a paginator object called p . This paginator creates page objects. Each Page object will have equal number of post objects. Then we retrieved the desired page number from the query parameter ‘page’ from a GET request. This page number is used to extract the correct page object.
Now in index.html –
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Django Paginator example</ title >
</ head >
< body >
< div class = "container" >
{% for post in page_obj.object_list %}
{# note that the list of posts are in the page_obj.object_list not page_obj #}
< h1 >{{post.title}}</ h1 >
< small >{{post.author}}</ small >
< p >{{post.content}}</ p >
< hr />
{% endfor %}
</ div >
< center >
{%if page_obj.has_previous %} {# whether the previous page exists #}
< a href = "?page={{page_obj.previous_page_number}}" ><</ a > {# link to the prev page #}
{% endif %}
< span >{{page_obj.number}}</ span > {# the current page number #}
{%if page_obj.has_next %} {# whether the next page exists #}
< a href = "?page={{page_obj.next_page_number}}" >></ a > {# link to the next page #}
{% endif %}
</ center >
</ body >
</ html >
|
Result :

In the image we have send the value of the page number with a GET request ( denoted with rectangle). You can see the pagination in the bottom of the image ( marked with rectangle ).
Here is another image of the last page –

Methods :
Paginator.get_page( page_number ) |
It takes a number argument and returns a Page object with the given 1-based index. If the passed argument is not a number then it returns the first page. If the page number is negative or a number that is greater than the number of pages then it returns the last page.
Throws EmptyPage error if the object list of the page is empty and allow_empty_first_page is set to false in Paginator object.
|
Paginator.page(number) |
It also does the same thing but raises InvalidPage error if the page of that number does not exist. |
Attributes :
Paginator.count |
returns the total number of objects across all pages. |
Paginator.num_pages |
returns the total number of pages |
Paginator.page_range |
returns a 1-based range iterator |
However, as Paginator class uses Page class to distribute objects, It will be better if we know more about the Page Class.
Page Class :
Generally Page object is used in Paginator Class. You rarely have to construct it manually.
Syntax :
page = Page( object_list , number, paginator)
Here object list is the list of objects , number argument is used to number the Page object. Paginator is the Paginator objects for whom this page object is constructed.
Attributes :
Page.object_list |
returns the list of objects |
Page.number |
returns the number of the page object |
Page.paginator |
returns the corresponding paginator object |
Methods :
Page.has_next() |
returns True if the next Page object exits else returns False |
Page.has_previous() |
returns True if the previous Page object exits else returns False |
Page.has_other_pages() |
Returns True if there’s a next or previous page. |
Page.next_page_number() |
Returns the next page number. Raises InvalidPage if next page doesn’t exist. |
Page.previous_page_number() |
Returns the previous page number. Raises InvalidPage if previous page doesn’t exist. |
Page.start_index() |
Returns the 1-based index of the first object on the page, relative to all of the objects in the paginator’s lis |
Page.end_index() |
Returns the 1-based index of the last object on the page, relative to all of the objects in the paginator’s list. |
Similar Reads
How To Do Pagination In Python
In this article, we'll walk you through the steps of setting up pagination in Python. We'll explain each step clearly and straightforwardly. To help you understand the idea of pagination in Python, we'll show you how to create a pagination system using the Tkinter library. We'll start by explaining
5 min read
Adding Pagination in APIs - Django REST Framework
Imagine you have huge amount of details in your database. Do you think that it is wise to retrieve all at once while making an HTTP GET request? Here comes the importance of the Django REST framework pagination feature. It facilitates splitting the large result set into individual pages of data for
8 min read
How to add AMP to Django Project?
A blog mostly needs content but that doesn't mean, your blog will be on top of Google search. For this you will need Speed, Security, user base and first of all search engines need to know that your blog exists. We will add AMP for speed. Â This article is in continuation of Blog CMS Project in Djang
4 min read
How to Create a Basic Project using MVT in Django ?
Prerequisite - Django Project MVT Structure Assuming you have gone through the previous article. This article focuses on creating a basic project to render a template using MVT architecture. We will use MVT (Models, Views, Templates) to render data to a local server. Create a basic Project: To initi
2 min read
How to Convert Models Data into JSON in Django ?
Django is a high-level Python based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. How to Convert Models
2 min read
Adding WYSIWYG editor to Django Project
Often, to manage content efficiently we use WYSIWYG (What You See Is What You Get) editor which stores our content in html and is also helpful to upload images, creating links, lists and works almost like Wordpress editor. This article is in continuation of Blog CMS Project in Django. Check this out
3 min read
How to Do SELECT MAX in Django?
When working with databases in Django, we often need to find the highest value of a specific field in a model. Let's say, find the book with the highest price, the product with the highest discount, etc. This is like doing a SELECT MAX query in SQL. In this article, we'll learn how to find the max v
4 min read
Connect Django Project to MongoDB
Djongo is a SQL to MongoDB query transpiler. Using djongo, we can use MongoDB as a backend database for our Django project. We don't even need to change the Django ORM. The best part is that we can setup Django with MongoDB by adding just one line of code. There is no need to change serializers, vie
2 min read
How to Add Cart in a Web Page using Django?
Adding a shopping cart to a web page using Django is a crucial step when building an e-commerce website or any other application that involves online transactions. A shopping cart allows users to collect and manage items they want to purchase before proceeding to checkout. In this tutorial, we'll wa
6 min read
How to Output Django QuerySet as JSON
In Django, a common task for web developers is to return data in JSON format, especially when working with APIs. A Django QuerySet, which is a collection of database queries, can be serialized and output as JSON. This article will guide us through how to output a Django QuerySet as JSON using two me
4 min read