Different Ways Of Pagination In Django

In this  Django pagination article, I will walk you through step by step how to create an app with different ways of pagination ie. Multiple Item pagination and Single Item Pagination

Django has a built-in pagination class that allows you to manage paginated data. Under the hood, all methods of pagination use the Paginator class. It does all the heavy lifting of actually splitting a QuerySet into Page objects.

We are going to begin by creating a new Django project using this command

django-admin startproject myproject

After creating the project change directory to the project folder directory and after that you need to create an App by executing the following command. I am going to call this app MyApp 

python startapp myapp 

After the app has been created, you will have to add it in the file where you have INSTALLED_APPS


OK now after this you need to do migrate your Django project

Python migrate

Let's create a templates folder because we need to add some HTML files to our templates folder, make sure that after creating templates folder you need to open your and add the templates directory into DIRS section like this.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates']

1. Multiple Items pagination

In this type of pagination, a page can contain one or more items not base on the items primary key or id

we are going to begin  our pagination process by creating our models as seen below

from django.db import models
from django.utils import timezone

class Items(models.Model):
    title = models.CharField(max_length = 200)
    author = models.CharField(max_length = 200)
    content = models.TextField()
    date_pub = models.DateTimeField(default =

    def __str__(self):
        return self.title

we have a model that has four fields.

After this, you have to make migrations and migrate

python makemigrations  
python migrate

Did this help you?

Not clear with something? drop a Comment

  • related posts



    leave a reply