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.  

models.py

from django.db import models

# Create your models here.

class MenuList(models.Model):
    Menucode= models.IntegerField()
    MenuType=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()

urls.py

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

urlpatterns = [
    path("",Menu_list,name="Employee"),    
]

views.py

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)

    try:
        result_page = pages.page(page_list)

    except PageNotAnInteger:
        result_page = pages.page(1)

    except EmptyPage:
        result_page = pages.page(pages.num_pages)

    return render(request,'menulist.html',{'menulist':result_page})
    
<html>
<head>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>     
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>     
   
</head>
<body>
  <div class="container"> 
    <div class="shadow p-3 mb-5 bg-grey rounded"> 

<table class="table table-bordered">
    <thead>
      <tr>
        <th> SLNo </th>
        <th> Menu Name</th>
        <th> Sub Menu Name</th>
      </tr>
    </thead>
 <tbody>
    {% for menu in menulist  %}
    <tr>
      <td>{{ menu.Menucode }}  </td>
      <td>{{ menu.menuname }}  </td>
      <td> {{ menu.submenuname }} </td>
    </tr>
    {% endfor %} 
 </tbody>
</table>
 
<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 %}

</ul>
</div>
</div>
</body>

</html>
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments