# md.py
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_response(self,request,response):
print('m1.process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_response(self, request, response):
print('m2.process_response')
return response
# setting.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
..........
'md.M1',
'md.M2',
]
# views.py
def test(request):
print('ok')
return HttpResponse('ok')
# 输出结果
m1.process_request
m2.process_request
ok
m2.process_response
m1.process_response
# md.py
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
def process_response(self,request,response):
print('m1.process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_response(self, request, response):
print('m2.process_response')
return response
# 输出结果
m1.process_request
m2.process_request
m1.process_view
ok
m2.process_response
m1.process_response
# md.py
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
def process_response(self,request,response):
print('m1.process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view')
def process_response(self, request, response):
print('m2.process_response')
return response
# 输出结果
m1.process_request
m2.process_request
m1.process_view
m2.process_view
ok
m2.process_response
m1.process_response
# md.py
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
response = callback(request,*callback_args, **callback_kwargs)
return response
def process_response(self,request,response):
print('m1.process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view')
def process_response(self, request, response):
print('m2.process_response')
return response
# 输出结果
m1.process_request
m2.process_request
m1.process_view
ok
m2.process_response
m1.process_response
# views.py 在视图函数中添加错误进行测试
def test(request):
print('ok')
int('test')
return HttpResponse('ok')
# md.py
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
# response = callback(request,*callback_args, **callback_kwargs)
# return response
def process_response(self,request,response):
print('m1.process_response')
return response
def process_exception(self, request, exception):
print('m1.process_exception')
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view')
def process_response(self, request, response):
print('m2.process_response')
return response
def process_exception(self, request, exception):
print('m2.process_exception')
# 输出结果
m1.process_request
m2.process_request
m1.process_view
m2.process_view
ok
m2.process_exception
m1.process_exception
m2.process_response
m1.process_response
应用
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
# response = callback(request,*callback_args, **callback_kwargs)
# return response
def process_response(self,request,response):
print('m1.process_response')
return response
def process_exception(self, request, exception):
print('m1.process_exception')
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view')
def process_response(self, request, response):
print('m2.process_response')
return response
def process_exception(self, request, exception):
print('m2.process_exception')
return HttpResponse('有错误!')
# 不会报错
# 输出结果
m1.process_request
m2.process_request
m1.process_view
m2.process_view
ok
m2.process_exception
m2.process_response
m1.process_response
# 对视图函数的返回值有一定的要求,在函数返回值中有render方法才执行。
# views.py
from django.shortcuts import render,redirect,HttpResponse
# Create your views here.
class JsonResponse:
def __init__(self,req,status,msg):
self.req = req
self.status = status
self.msg = msg
def render(self):
import json
ret = {
'status': self.status,
'msg': self.msg
}
return HttpResponse(json.dumps(ret))
def test(request):
return JsonResponse(request,True,'错误信息!')
# md.py
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
# response = callback(request,*callback_args, **callback_kwargs)
# return response
def process_response(self,request,response):
print('m1.process_response')
return response
def process_exception(self, request, exception):
print('m1.process_exception')
def process_template_response(self,request,response):
print('m1.process_template_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view')
def process_response(self, request, response):
print('m2.process_response')
return response
def process_exception(self, request, exception):
print('m2.process_exception')
return HttpResponse('有错误!')
# 输出结果
m1.process_request
m2.process_request
m1.process_view
m2.process_view
m1.process_template_response
m2.process_response
m1.process_response