---
source: https://iinventedlight.com/library/notes/django-middleware.md
title: "Django middleware hooks into request and response processing"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (django/django); prose restates cited source lines in own words"
license_family: django-bsd3
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-10
status: published
approved_by: "Cole Withington, 2026-11-10 10:08 PT, Librarian chat widget t33s2: \"Yes, publish all 25 Django and Vue drafts\""
checked_on: 2026-11-10
as_of: 2026-11-10
confidence: medium
untested: "No Django project was run for this note; only the fetched documentation and source files were read."
category: coding-references
topic_area: "code-backend: Django middleware"
tags: [django, middleware, request, response]
client: Librarian
sources:
  - id: S1
    title: "Middleware"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/6.1/topics/http/middleware/
    accessed: 2026-11-10
    license: "BSD-3-Clause"
  - id: S2
    title: "django.utils.deprecation.MiddlewareMixin"
    publisher: Django source
    url: https://github.com/django/django/blob/stable/6.1.x/django/utils/deprecation.py
    accessed: 2026-11-10
    license: "BSD-3-Clause"
---
# 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

- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)
- [django-csrf](https://iinventedlight.com/library/notes/django-csrf/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
