How to Add Pagination in Django Python

In this tutorial we will discuss How to Add Pagination in Django Python Web application.

What is Pagination – Pagination is the process of distributing a website’s content across multiple pages.

Django has a  built in Paginator class.

How to Use – First we need to import it “Paginator class” from django.core.paginator.

from django.db import models

# Create your models here.

class MenuList(models.Model):
    Menucode= models.IntegerField()
    menuname = models.CharField(max_length=100)
    submenuname = models.CharField(max_length=100)
    menulink = models.CharField(max_length=100)

    def __str__(self):
        return self.menuname
    objects = models.Manager()

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

urlpatterns = [

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

# Create your views here.

def Menu_list(request):

    Result_set = MenuList.objects.all()
    page_list = request.GET.get('page',1)
    pages = Paginator(Result_set,5)

        result_page =

    except PageNotAnInteger:
        result_page =

    except EmptyPage:
        result_page =

    return render(request,'menulist.html',{'menulist':result_page})
      <link rel="stylesheet" href="">
      <script src=""></script>     
      <script src=""></script>     
  <div class="container"> 
    <div class="shadow p-3 mb-5 bg-grey rounded"> 

<table class="table table-bordered">
        <th> SLNo </th>
        <th> Menu Name</th>
        <th> Sub Menu Name</th>
    {% for menu in menulist  %}
      <td>{{ menu.Menucode }}  </td>
      <td>{{ menu.menuname }}  </td>
      <td> {{ menu.submenuname }} </td>
    {% endfor %} 
<ul class="pagination">
  {% if menulist.has_previous %}
    <li class="page-item"><a href="?page={{ menulist.previous_page_number }}">Previous</a></li>
  {% else %}
    <li class="page-item disabled"><span>Previous</span></li>
  {% endif %}
  {% for k in menulist.paginator.page_range %}
   {% if menulist.number == k %}
   <li class="page-item active"><span>{{ k }}<span class="sr-only">(current)</span></span></li>
   {% else %}
   <li class="page-item"><a href="?page={{k}}">{{k}}</a></li> 
   {% endif %}
   {% endfor %}
   {% if menulist.has_next %}
   <li class="page-item"><a href="?page={{ menulist.next_page_number }}">Next</a></li> 
   {% else %}
   <li class="page-item disabled"><span>Next</span></li>
   {% endif %}


0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments