python
37 lines · 7 steps
Filtering and paginating a Django product list
A view builds a lazy queryset step by step from URL parameters, then paginates it for the template.
Explained by
highlit
1from django.core.paginator import Paginator
2from django.shortcuts import render
3
4from .models import Product
5
6
7def product_list(request):
8 products = Product.objects.select_related("category").order_by("-created_at")
9
10 category = request.GET.get("category")
11 if category:
12 products = products.filter(category__slug=category)
13
14 search = request.GET.get("q", "").strip()
15 if search:
16 products = products.filter(name__icontains=search)
17
18 in_stock = request.GET.get("in_stock")
19 if in_stock == "1":
20 products = products.filter(stock__gt=0)
21
22 paginator = Paginator(products, 24)
23 page = paginator.get_page(request.GET.get("page"))
24
25 querystring = request.GET.copy()
26 querystring.pop("page", None)
27
28 return render(
29 request,
30 "catalog/product_list.html",
31 {
32 "page": page,
33 "querystring": querystring.urlencode(),
34 "category": category,
35 "search": search,
36 },
37 )
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Django querysets are lazy, so chaining filters conditionally builds no database work until the page is actually sliced.
- 2Reading filters straight from request.GET lets one view handle category, search, and stock combinations without extra routes.
- 3Stripping the page key from a copied querystring keeps pagination links carrying every other active filter.
Related explainers
python
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 steps
python
from django import forms from django.utils import timezone from .models import Reservation
Multi-field validation in a Django ModelForm
form validation
cross-field validation
modelform
Intermediate
7 steps
python
from django import template from django.urls import reverse, NoReverseMatch from django.utils.html import format_html
Active nav-link template tags in Django
template tags
url routing
active state
Intermediate
7 steps
ruby
module Paginatable extend ActiveSupport::Concern private
A reusable pagination concern in Rails
pagination
concerns
http-headers
Intermediate
8 steps
python
from functools import wraps import asyncio from fastapi import APIRouter, FastAPI, Request
Per-route request timeouts in FastAPI
decorators
async
timeouts
Intermediate
6 steps
python
import os from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent.parent
How a Django settings module is wired
configuration
environment-variables
middleware
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/filtering-and-paginating-a-django-product-list-explained-python-3284/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.