graph LR
Component_Configuration["Component Configuration"]
BaseHandler["BaseHandler"]
MiddlewareChain["MiddlewareChain"]
Middleware["Middleware"]
HttpRequest["HttpRequest"]
View["View"]
HttpResponse["HttpResponse"]
Unclassified["Unclassified"]
BaseHandler -- "Reads" --> Component_Configuration
BaseHandler -- "Builds" --> MiddlewareChain
MiddlewareChain -- "Composed of" --> Middleware
HttpRequest -- "Passes through" --> MiddlewareChain
Middleware -- "Processes" --> HttpRequest
MiddlewareChain -- "Invokes" --> View
View -- "Generates" --> HttpResponse
HttpResponse -- "Passes back through" --> MiddlewareChain
Middleware -- "Processes" --> HttpResponse
click Middleware href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django/Middleware.md" "Details"
The central flow begins when an incoming HttpRequest is received by the BaseHandler. The BaseHandler dynamically constructs a MiddlewareChain based on the MIDDLEWARE setting in the project's configuration. This chain is a nested structure of all defined middleware. The request is passed through each middleware in the defined order, allowing each to process or modify it. After passing through the entire chain, the request reaches the target View, which contains the primary application logic. The View processes the request and generates an HttpResponse. This response then travels back through the MiddlewareChain in reverse order, allowing each middleware to process or modify it before it is sent to the client. This two-phase processing—first the request, then the response—is the fundamental pattern of Django's middleware architecture.
A project-level setting that defines the ordered list of middleware classes to be applied to every request. This configuration is the blueprint used by the BaseHandler to construct the middleware chain.
Related Classes/Methods:
The core request orchestrator. It loads the middleware classes specified in the configuration and builds the middleware chain. It then initiates the request-response cycle by passing the request to the chain.
Related Classes/Methods:
A composite callable representing the full stack of middleware. It is constructed by the BaseHandler, wrapping each middleware in sequence to process the request on its way to the view and the response on its way back.
Related Classes/Methods:
Middleware [Expand]
A single, pluggable class that hooks into the request/response cycle. Each middleware can modify the HttpRequest before it reaches the view or the HttpResponse before it's sent to the client.
Related Classes/Methods:
An object encapsulating the incoming HTTP request. It is created by the server handler and passed through the MiddlewareChain to the view, carrying all request data.
Related Classes/Methods:
The endpoint for a request after it has passed through the middleware chain. The view executes the primary application logic and is responsible for generating the HttpResponse.
Related Classes/Methods:
An object encapsulating the outgoing HTTP response. It is created by the View and then travels back through the MiddlewareChain in reverse order, allowing each middleware to process it.
Related Classes/Methods:
Component for all unclassified files and utility functions (Utility functions/External Libraries/Dependencies)
Related Classes/Methods: None