Writing your own middleware
Writing your own middleware is easy. Each middleware component is a single Python class that defines one or more of the following methods:
process_request
Method: process_request(request)
request
is anHttpRequest
object.process_request()
is called on each request, before Django decides which view to execute.
It should return either None
or an HttpResponse
object. If it returns None
, Django will continue processing this request, executing any other process_request()
middleware, then, process_view()
middleware, and finally, the appropriate view.
If it returns an HttpResponse
object, Django won't bother calling any other request, view or exception middleware, or the appropriate view; it'll apply response middleware to that HttpResponse
, and return the result.
process_view
Method: process_view(request, view_func, view_args, view_kwargs)
request
is anHttpRequest
object.view_func
is the Python function that Django is about to use. (It's the actual function object, not...