Effortless Pagination and Search in Django: A Beginner’s Guide

Prosenjeet Shil
Django Unleashed
Published in
4 min readJun 12, 2024

What is Pagination and Why Use It?

What is Pagination?

Pagination is a technique used in web applications to divide a large dataset into smaller, more manageable chunks or pages. Instead of loading and displaying all the data at once, which can overwhelm users and significantly slow down page load times, pagination allows data to be presented in smaller sections, often with navigation controls to move between pages.

Why Use Pagination?

  • Improved Performance: Loading a large amount of data all at once can be slow and inefficient. Pagination helps improve the performance of your application by fetching and displaying only a small subset of data at a time.
  • Enhanced User Experience: Users can navigate through the data more easily and find what they are looking for without being overwhelmed by too much information at once.
  • Better Control: Pagination provides better control over the layout and design of your application, allowing for more organized and aesthetically pleasing data presentation.
  • Reduced Bandwidth Usage: By only loading the data that is needed for the current page, pagination helps reduce the bandwidth usage, which can be crucial for applications with limited resources or those accessed over slow internet connections.

Note: This article assumes you have a good understanding of Django and how to create a basic CRUD application. If you’re new to Django or need a refresher on building a CRUD application, you can read this comprehensive guide on building a Django CRUD application.

Adding Pagination to a Django Project

Let’s walk through the steps to add pagination to a Django project.

  1. Setting Up the Django Model
    In the models.py file, we define a Movies model with fields for the movie name and rating. This model represents the data structure for storing movie information.
from django.db import models

class Movies(models.Model):
name = models.CharField(max_length=50)
rating = models.FloatField()

def __str__(self):
return f'{self.name}'

2. Creating the View for Pagination
In views.py, we create a movielist view function to handle pagination.

# views.py
from django.shortcuts import render
from .models import Movies
from django.core.paginator import Paginator

def movielist(request):
obj = Movies.objects.all()

paginator = Paginator(obj, 4) # Show 4 movies per page
page = request.GET.get('page')
obj = paginator.get_page(page)

context = {'obj': obj}
template_name = 'myapp/movie_list.html'
return render(request, template_name, context)
  • Fetching Data: Retrieve all movie objects from the database using Movies.objects.all().
  • Pagination: Create a Paginator object with the movie list and specify that we want to display 4 movies per page. Get the current page number from the request and fetch the corresponding page of movie objects.
  • Context and Template Rendering: Pass the paginated movie objects to the template for rendering.

3. Creating the Template for Displaying Data
In movie_list.html, we create the HTML structure to display the movies and the pagination controls.

<!-- movie_list.html -->
{% for movie in obj %}
{{ movie.name }}
<br>
{% endfor %}

{% if obj.has_previous %}
<a href="?page=1">First</a>
<a href="?page={{ obj.previous_page_number }}">Previous</a>
{% endif %}

page: {{ obj.number }} of {{ obj.paginator.num_pages }}

{% if obj.has_next %}
<a href="?page={{ obj.next_page_number }}">Next</a>
<a href="?page={{ obj.paginator.num_pages }}">Last</a>
{% endif %}
  • Movie List: The movies are displayed using a loop that iterates over the paginated movie objects.
  • Pagination Controls: Navigation links are provided to move to the first, previous, next, and last pages. These links are dynamically generated based on the presence of previous and next pages.

4. Configuring URLs
In the urls.py files, we define the URL patterns to map the view function to a specific URL.

# myapp/urls.py
from django.urls import path
from .views import movielist

urlpatterns = [
path('movielist/', movielist, name='list'),
]
# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]

Adding Search Functionality

  1. Enhancing the View with Search
    We update the movielist view function in views.py to handle search functionality.
# views.py
from django.shortcuts import render
from .models import Movies
from django.core.paginator import Paginator

def movielist(request):
obj = Movies.objects.all()

# Search
movie_name = request.GET.get('movie_name')
if movie_name and movie_name.strip():
obj = obj.filter(name__icontains=movie_name)

paginator = Paginator(obj, 4) # Show 4 movies per page
page = request.GET.get('page')
obj = paginator.get_page(page)

context = {'obj': obj}
template_name = 'myapp/movie_list.html'
return render(request, template_name, context)

Search Functionality: Check if the movie_name parameter is present in the request. If it is, filter the movie objects using name__icontains, which performs a case-insensitive match on the movie name.

2. Updating the Template for Search
We enhance movie_list.html to include a search form.

<form method="get">
<input type="search" name="movie_name">
<button type="submit">Search</button>
</form>

{% for movie in obj %}
{{movie.name}}
<br>
{% endfor %}

{% if obj.has_previous %}
<a href="?page=1">First</a>
<a href="?page={{ obj.previous_page_number }}">Previous</a>
{% endif %}

page: {{ obj.number }} of {{ obj.paginator.num_pages }}

{% if obj.has_next %}
<a href="?page={{ obj.next_page_number }}">Next</a>
<a href="?page={{ obj.paginator.num_pages }}">Last</a>
{% endif %}

Search Form: A form is provided at the top to allow users to search for movies by name.

Conclusion

By following these steps, you can add efficient pagination and search functionality to your Django application. This not only enhances the user experience but also ensures your application performs optimally by handling large datasets gracefully.

Happy coding!

--

--

Prosenjeet Shil
Django Unleashed

Python developer sharing insights on full stack development: Python, database management, Django, DRF, React JS, and more. Follow for tips and tutorials.