程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Middleware of Django

編輯:Python

 

Middleware can define five methods

Namely :( The main thing is process_request and process_response)

  • process_request(self,request)
  • process_view(self, request, view_func, view_args, view_kwargs)
  • process_template_response(self,request,response)
  • process_exception(self, request, exception)
  • process_response(self, request, 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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved