Writing Custom Middleware
Hook into every request and response.
Writing Custom Middleware is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Middleware in a Sentence
Middleware is a layer that wraps every request and response, letting you run code before a view and after it returns.
The Onion Model
Think of middleware as layers of an onion around your view. A request travels inward through each layer, then the response travels back out.
The Modern Style
A modern middleware is a callable that takes get_response once at startup and returns the handler Django calls per request.
A Minimal Middleware
The factory stores get_response, and the inner function runs your code around the call. This callable shape is all Django needs.
def simple(get_response):
def handler(request):
# before the view
response = get_response(request)
# after the view
return response
return handlerCode Before the View
Anything before get_response runs on the way in. It is perfect for logging, timing, or attaching data to the request.
def handler(request):
request.start = time.time()
return get_response(request)Code After the View
Anything after get_response runs on the way out. Here you can read or modify the response before it reaches the browser.
response = get_response(request)
response["X-App"] = "demo"
return responseThe Class-Based Form
You can also write middleware as a class with __init__ and __call__. It mirrors the function form but groups related hooks neatly.
class Timer:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)Register in MIDDLEWARE
Middleware only runs once you list its dotted path in the MIDDLEWARE setting inside settings.py.
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"myapp.middleware.simple",
]Short-Circuiting a Request
If middleware returns a response itself instead of calling get_response, it short-circuits the chain and the view never runs.
One Job Per Middleware
Keep each middleware focused on a single concern, like timing or auth checks. Small layers are easier to order and debug.
Common Real Uses
Teams use custom middleware for request logging, adding headers, locale handling, and lightweight auth checks across the whole site.
Quick Check
Check how a modern middleware hands off to the next layer.
Recap: Building Middleware
You built custom middleware: a callable taking get_response, with code before and after the view, registered in the MIDDLEWARE setting. ✅
Frequently asked questions
Is the “Writing Custom Middleware” lesson free?
Yes — the full text of “Writing Custom Middleware” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing Custom Middleware”?
Hook into every request and response. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing Custom Middleware” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- post_save and pre_delete Signals
- Connecting Receivers Properly
- Writing Custom Middleware
- Middleware Order and process_view