← All notes

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

Sources

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

Permanent ID: iil:note:kf8d53yc

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.},
}

Markdown

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.