Post

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 manage.py startapp myapp 

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
]

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

Python manage.py 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 settings.py and add the templates directory into DIRS section like this.

TEMPLATES = [
    {
        '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 = timezone.now())

    def __str__(self):
        return self.title

we have a model that has four fields.

After this, you have to make migrations and migrate

#1 
python manage.py makemigrations  
#2
python manage.py migrate

Did this help you?

  
Not clear with something? drop a Comment
author

  • related posts

    298 comments

    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author
    author

    leave a reply