标签:分享图片 之间 运行 set message short dep custom 返回
中间件是介于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 # 用于返回数据
标签:分享图片 之间 运行 set message short dep custom 返回
原文地址:https://www.cnblogs.com/q1ang/p/10098684.html