๐ Introduction
When there are many records in a database table, showing everything on one page is not a good idea. The page becomes long and difficult to read.
Pagination solves this problem by:
- showing only a few records on each page
- providing page navigation like Previous, Next, First, and Last
In this program, we will display student records page by page.
๐ฏ Program Statement
๐ Implement pagination for listing records.
๐ง Concept
This program uses:
- Django
Paginator - model records
- current page number from URL
- template navigation links
โ๏ธ Step 1: Use Existing Model
๐ File: models.py
๐น Path:
myproject/myapp/models.py
๐น Code:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
course = models.CharField(max_length=100)
email = models.EmailField(unique=True)
def __str__(self):
return self.name
โ๏ธ Step 2: Create View for Pagination
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
from django.core.paginator import Paginator
from .models import Student
def student_pagination(request):
student_list = Student.objects.all().order_by('id')
paginator = Paginator(student_list, 3) # 3 records per page
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'student_pagination.html', {
'page_obj': page_obj
})
๐ง Explanation
Student.objects.all().order_by('id')fetches all student records in orderPaginator(student_list, 3)divides records into pages with 3 records eachrequest.GET.get('page')reads the page number from the URLget_page()returns the requested page safely
โ๏ธ Step 3: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('student-pagination/', views.student_pagination, name='student_pagination'),
]
โ๏ธ Step 4: Create Template
๐ File: student_pagination.html
๐น Path:
myproject/templates/student_pagination.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Student Pagination</title>
</head>
<body>
<h1>Student Records with Pagination</h1>
{% if page_obj %}
<table border="1" cellpadding="10">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Course</th>
<th>Email</th>
</tr>
{% for s in page_obj %}
<tr>
<td>{{ s.id }}</td>
<td>{{ s.name }}</td>
<td>{{ s.age }}</td>
<td>{{ s.course }}</td>
<td>{{ s.email }}</td>
</tr>
{% endfor %}
</table>
<br>
<div>
{% if page_obj.has_previous %}
<a href="?page=1">First</a>
<a href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
<span>
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">Next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
</div>
{% else %}
<p>No records found.</p>
{% endif %}
</body>
</html>
๐ง Explanation
page_objcontains only the records for the current page{% for s in page_obj %}displays those recordshas_previousandhas_nextare used to show navigation linkspage_obj.numbershows the current pagepage_obj.paginator.num_pagesshows the total number of pages
โ๏ธ Step 5: Run Server
python manage.py runserver
๐ Step 6: Output
Open:
๐ http://127.0.0.1:8000/student-pagination/
If you have 10 records and page size is 3, then:
- Page 1 โ records 1 to 3
- Page 2 โ records 4 to 6
- Page 3 โ records 7 to 9
- Page 4 โ record 10
Example URL for second page:
http://127.0.0.1:8000/student-pagination/?page=2
๐ง How It Works
- View fetches all student records
Paginatorsplits them into pages- URL provides page number
- View sends selected page to template
- Template displays only those records
- Navigation links let user move across pages
๐ฅ Key Concepts
Import Paginator
from django.core.paginator import Paginator
Create Paginator
paginator = Paginator(student_list, 3)
Get Current Page Number
page_number = request.GET.get('page')
Get Page Object
page_obj = paginator.get_page(page_number)
Loop Through Page Records
{% for s in page_obj %}
โ ๏ธ Common Errors
โ Forgot to import Paginator
Django will show an import error.
โ Used students instead of page_obj in template
You must loop through:
{% for s in page_obj %}
โ Page links not working
Check that the URL contains:
?page=2
โ No ordering in queryset
Use:
Student.objects.all().order_by('id')
โ No records in database
Then pagination will not display meaningful results.
๐งช Practice Questions
- Change page size from 3 to 5
- Show clickable page numbers
- Add serial number column
- Add Bootstrap styling to pagination
- Combine pagination with search
๐ค Viva Questions & Answers
1. What is pagination in Django?
Pagination is the process of dividing many records into smaller pages for easy display and navigation.
2. Why do we use pagination?
Pagination improves readability and performance by preventing too many records from appearing on one page.
3. What is the Paginator class?
Paginator is a Django class used to divide a queryset or list into multiple pages.
4. What does Paginator(student_list, 3) mean?
It means only 3 records will be shown on each page.
5. What is page_obj?
page_obj is the object returned by paginator for the current page. It contains records and pagination information.
6. What is request.GET.get('page') used for?
It gets the current page number from the URL query string.
7. What is the use of has_previous?
It checks whether a previous page exists.
8. What is the use of has_next?
It checks whether a next page exists.
9. What does page_obj.number return?
It returns the current page number.
10. Why is pagination important in real applications?
Because real applications often contain large amounts of data, and pagination makes the interface cleaner and faster.
๐ Navigation
๐ Next Post: Register a Model in Django Admin and Display Records
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
