【Django】中間件
阿新 • • 發佈:2018-12-10
一道 ont ESS app wmi 文件 需要 技術 返回值
中間件是介於request與response處理之間的一道處理過程,在全局上改變django的輸入與輸出
Django默認的Middleware:
MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, ‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ]
當用戶發起請求的時候會依次經過所有的的中間件,這個時候的請求為process_request,最後到達views的函數中,views函數處理後,再依次穿過中間件,這個時候是process_response,最後返回給請求者
自定義中間件:
首先需要自己創建一個py文件,包含自定義的Middleware類(繼承MiddlewareMixin)
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class CustomerMiddleware(MiddlewareMixin): def process_request(self, request): print(‘CustomerMiddleware process_request...‘) # return HttpResponse(‘<h1>forbidden</h1>‘) def process_response(self, request, response): print(‘CustomerMiddleware process_response...‘) print(‘request:‘, request) print(‘response:‘, response) return response def process_view(self, request, callback, callback_args, callback_kwargs): print(‘CustomerMiddleware process_view‘) print(‘callback(視圖函數):‘, callback) def process_exception(self, request, exception): print(‘CustomerMiddleware process_exception‘) class CustomerMiddleware2(MiddlewareMixin): def process_request(self, request): print(‘CustomerMiddleware2 process_request...‘) def process_response(self, request, response): print(‘CustomerMiddleware2 process_response...‘) print(‘request:‘, request) print(‘response:‘, response) return response def process_view(self, request, callback, callback_args, callback_kwargs): print(‘CustomerMiddleware2 process_view‘) def process_exception(self, request, exception): print(‘CustomerMiddleware2 process_exception‘) return HttpResponse(‘<h3>Error:%s</h3>‘ % exception)
在settings.py配置自定義的中間件:
MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, ‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ‘app01.MyMiddleware.CustomerMiddleware‘, ‘app01.MyMiddleware.CustomerMiddleware2‘, ]
運行結果:
CustomerMiddleware process_request...
CustomerMiddleware2 process_request...
CustomerMiddleware process_view
callback(視圖函數): <function index at 0x0471F3D8>
CustomerMiddleware2 process_view
view...
CustomerMiddleware2 process_response...
request: <WSGIRequest: GET ‘/index/‘>
response: <HttpResponse status_code=200, "text/html; charset=utf-8">
CustomerMiddleware process_response...
request: <WSGIRequest: GET ‘/index/‘>
response: <HttpResponse status_code=200, "text/html; charset=utf-8">
中間件包含這四種方法:
process_request # 請求接收請求數據
process_view # process_request執行後,依次執行process_view,最後調用視圖函數,如果有返回值,會越過其他的process_view以及視圖函數,但是所有的process_response都還會執行。
process_exception # 在視圖函數執行出錯的時候才會執行該方法
process_response # 用於返回數據
【Django】中間件