Django middleware hooks into request and response processing
Answer: Middleware is a framework of hooks into Django's request/response processing. Each component is a low-level plugin that can change Django's input or output globally.
Shape
A middleware factory takes a get_response callable and returns middleware. Middleware itself takes a request and returns a response, like a view. You can write it as a function or as a callable class.
Activation and order
You activate a component by adding its dotted path to the MIDDLEWARE setting. Order matters because one middleware can depend on another. For example, AuthenticationMiddleware stores the user in the session, so it must run after SessionMiddleware.
Mixin
MiddlewareMixin requires get_response. If that callable is a coroutine function, the mixin turns on async mode so a thread is not held for the whole request.
See also
Claims
- id: C1 claim: Middleware is a hook framework around request and response processing. source: S1 evidence: "Middleware is a framework of hooks into Django’s request/response processing."
- id: C2 claim: You activate middleware by adding it to the MIDDLEWARE list. source: S1 evidence: "To activate a middleware component, add it to the MIDDLEWARE list"
- id: C3 claim: MIDDLEWARE order matters because components can depend on each other. source: S1 evidence: "The order in MIDDLEWARE matters because a middleware can depend"
- id: C4 claim: MiddlewareMixin requires a get_response callable. source: S2 evidence: "get_response must be provided."
- id: C5 claim: If get_response is a coroutine, the mixin switches to async mode. source: S2 evidence: "If get_response is a coroutine function, turns us into async mode so"
Sources
- [S1] Middleware. Django docs. https://docs.djangoproject.com/en/6.1/topics/http/middleware/ . License: BSD-3-Clause. Accessed 2026-11-10.
- [S2] django.utils.deprecation.MiddlewareMixin. Django source. https://github.com/django/django/blob/stable/6.1.x/django/utils/deprecation.py . License: BSD-3-Clause. Accessed 2026-11-10.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: Django documentation and source (django/django): Copyright (c) Django Software Foundation and individual contributors; BSD-3-Clause (https://raw.githubusercontent.com/django/django/stable/6.1.x/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Middleware, BSD-3-Clause (django/django); Django source, django.utils.deprecation.MiddlewareMixin, BSD-3-Clause (django/django). Notices: Django documentation and source (django/django): Copyright (c) Django Software Foundation and individual contributors; BSD-3-Clause (https://raw.githubusercontent.com/django/django/stable/6.1.x/LICENSE).
Cite this
iinventedlight. 2026. "Django middleware hooks into request and response processing." iinventedlight Library, iil:note:kf8d53yc, version of 2026-11-10. https://iinventedlight.com/library/notes/django-middleware/. License: CC-BY-4.0.
@misc{iil_note_kf8d53yc,
author = {{iinventedlight}},
title = {{Django middleware hooks into request and response processing}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-10},
url = {https://iinventedlight.com/library/notes/django-middleware/},
note = {Permanent ID iil:note:kf8d53yc. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-middleware.md.