Hooks and application order
During the request phase, before calling the view, Django applies middleware in the order it's defined in MIDDLEWARE_CLASSES
, top-down. Two hooks are available:
process_request()
process_view()
During the response phase, after calling the view, middleware are applied in reverse order, from the bottom up. Three hooks are available:
process_exception()
process_template_response()
process_response()
If you prefer, you can also think of it like an onion: each middleware class is a layer that wraps the view.
The behavior of each hook is described below.
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.