Django Python Pagination Tutorial Example

Published On: 05/07/2022 | Category: Django Python

Hi Dev,

In this tutorial, I will show you django pagination example. we will help you to give example of django python pagination bootstrap 4 example. In this article, we will implement a how to add pagination in python django. this example will help you how to create pagination in django. So, let's follow few step to create example of how to include pagination in django.

Here i explained simply step by step example of how to how to create pagination in django.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp pagination
Step 3 : Update

In this step we require to do two things in our file, One is to change the path of template look up directory. Second one is to configure our media folder. Add the below lines to your file:

Next, you need to add it in the file as follows:

import os

Step 4 : Database Setup

Next step, we will modify the file and update the database settings to configure the mydb database:
    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  
Step 5 : Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the pagination/ file and add:

from django.shortcuts import render
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.

def index(request):
    posts = Post.objects.all()
    page = request.GET.get('page', 1)

    paginator = Paginator(posts, 3)
        posts =
    except PageNotAnInteger:
        posts =
    except EmptyPage:
        posts =

    return render(request, 'index.html', { 'posts': posts })
Step 6 : Creating the Views

Next, open the pagination/templates/index.html file and the add:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Python Django Pagination Example</title>
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <script src="[email protected]/dist/jquery.slim.min.js"></script>
    <script src="[email protected]/dist/umd/popper.min.js"></script>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="" />
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-md-9">
                                <h3>Python Django Pagination Example - <span class="text-primary"></span></h3>
                    <div class="card-body">
                        <table class="table table-bordered table-hover">
                                    <th width="150">Title</th>
                                    <th width="200">Content</th>
                                {% for post in posts %}
                                        <td>{{ post.title }}</td>
                                        <td>{{ post.description }}</td>
                                    {% empty %}
                                        <tr class="text-center">
                                            <td colspan="4">There are no Record Found!</td>
                                {% endfor %}

                        <nav aria-label="Page navigation example">
                          <ul class="pagination justify-content-end">
                            {% if posts.has_previous %}
                              <li class="page-item">
                                <a class="page-link" href="?page={{ posts.previous_page_number }}">Previous</a>
                            {% else %}
                              <li class="page-item disabled">
                                <a class="page-link" href="#" tabindex="-1" aria-disabled="True">Previous</a>
                            {% endif %}
                            {% for i in posts.paginator.page_range %}
                              {% if posts.number == i %}
                                <li class="page-item active" aria-current="page">
                                  <span class="page-link">
                                    {{ i }}
                                    <span class="sr-only">(current)</span>
                              {% else %}
                                <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
                              {% endif %}
                            {% endfor %}
                            {% if posts.has_next %}
                              <li class="page-item">
                                <a class="page-link" href="?page={{ posts.next_page_number }}">Next</a>
                            {% else %}
                              <li class="page-item disabled">
                                <a class="page-link" href="#" tabindex="-1" aria-disabled="True">Next</a>
                            {% endif %}
                        <!--end of Pagination-->

Step 7 : Creating Urls

In this section, we’ll create the urls to access our views.Go to the pagination/ file and update it as follows:

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('', index, name = 'index'),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....