Process_view execution order is also in order to perform settings.py
After urls.py process_view correspondence relationship, the functions performed before view
If it returns None, proceed to the back of the middleware functions process_view
If the HttpResponse returns, subsequent process_view function is not performed, skip to a function execution process_response
middleware_test.py:
Import MiddlewareMixin django.utils.deprecation from from django.shortcuts Import HttpResponse class the Test (MiddlewareMixin): DEF process_request (Self, Request): Print ( "It is a middleware -> the Test") DEF process_response (Self, Request, the Response ): Print ( "this is the Test of HttpResponse") return HttpResponse ( "this is a Test return HttpResponse") DEF process_view (Self, Request, view_func, view_args, view_kwargs): '' ' : param Request: browser sent request request object : param view_func: the name of the view function to be performed : param view_args: location parameter view function to be performed : param view_kwargs: keywords view function to be executed : return: ''' print ( "This is the Test of process_view function") Print (view_func, of the type (view_func)) class Test2 (MiddlewareMixin): DEF process_request (Self, Request): print ( "This is a middleware -> test2") DEF process_response (Self, Request, the Response): Print ( "here Test2's HttpResponse") return HttpResponse ( "this is the Test2 return HttpResponse") DEF process_view (selfm, Request, view_func, view_args, view_kwargs): Print ( "this is the Test2 the process_view function ") Print (view_func, of the type (view_func))
views.py:
django.shortcuts Import HttpResponse from DEF index (Request): Print ( "This is the index page") return HttpResponse ( "Here is the main page index")
Visit, http: //127.0.0.1: 8000 / index /
operation result:
Implementation process:
Process_request function is performed first, and then perform process_view function before performing the function view, and then execute the view function, a function performed last process_response
process_request only returns None, after have been performed, it matches the route, find the view function to be executed, the first execution middleware process_view function before performing the function view
If after return process_view None, continuing with the subsequent method process_view middleware, after executing all of the functions executed view function process_view
If there is a process_view returned HttpResponse, do not perform subsequent process_view function, it will jump to the first process_response function, and continue down
For example, the intermediate return the HttpResponse process_view 3, no longer perform the subsequent intermediate 4,5,6 middleware, the middleware process_response direct jump function 6, and then perform intermediate 5,4,3, 2,1
If there process_request function returns HttpResponse object, do not perform subsequent process_request function is not performed process_view function, jump to the corresponding function is executed process_response