Namely :( The main thing is process_request and process_response)
The return value of the above method can be None Or a HttpResponse object , If it is None, Continue to follow django The defined rules continue backward , If it is HttpResponse object , Return the object directly to the user .
process_request
process_request There is a parameter , Namely request, This request And in view functions request It's the same .
Its return value can be None It can also be HttpResponse object . The return value is None Words , Follow the normal process , To the next Middleware , If it is HttpResponse object ,Django View function will not be executed , And return the corresponding object to the browser .
process_view
process_view(self, request, view_func, view_args, view_kwargs)
request yes HttpRequest object
view_func yes Django View functions to be used .( It's the actual function object , Instead of the name of the function as a string )
view_args Is the list of location parameters that will be passed to the view
view_kwargs Is the dictionary of key parameters that will be passed to the view . view_args and view_kwargs Does not contain the first view parameter (request)
Django It will be called before calling the view function. process_view Method
It should return None Or a HttpResponse object . If you return None,Django This request will continue to be processed , Execute any other middleware process_view Method , Then execute the corresponding view . If it returns a HttpResponse object ,Django The corresponding view function will not be called . It will execute the process_response Method and will be applied to the HttpResponse And return the result .
process_exception
process_exception(self, request, exception)
One HttpRequest object
One exception It is caused by view function exception Exception object .
This method can only be executed if there is an exception in the view function , It can return a value of None It can also be a HttpResponse object . If it is HttpResponse object ,Django The process_response Method , And return to browser , Otherwise, the exception will be handled by default . If you return one None, The next Middleware process_exception Method to handle exceptions . Its execution order is also the reverse of middleware registration order .
process_template_response
It's no use
process_template_response(self, request, response)
One HttpRequest object
response yes TemplateResponse object ( Generated by view function or middleware )
process_template_response It is executed immediately after view function execution , But it has a precondition , That is, the object returned by the view function has a render() Method ( Or indicate that the object is a TemplateResponse Object or equivalent method )
class MD1(MiddlewareMixin):
def process_request(self, request):
print("MD1 Inside process_request")
def process_response(self, request, response):
print("MD1 Inside process_response")
return response
def process_view(self, request, view_func, view_args, view_kwargs):
print("-" * 80)
print("MD1 Medium process_view")
print(view_func, view_func.__name__)
def process_exception(self, request, exception):
print(exception)
print("MD1 Medium process_exception")
return HttpResponse(str(exception))
def process_template_response(self, request, response):
print("MD1 Medium process_template_response")
return response
項目介紹隨著時代的發展,The number of coll
Resolve the error UnicodeDecod