Django自定义中间件
from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
class MD1(MiddlewareMixin):
def process_request(self, request):
print('到达视图函数前')
return None
def process_view(self, request, view_func, view_args, view_kwargs):
print('到达视图函数前,process_request后')
return None
def process_response(self, request, response):
print('视图函数响应后')
return response
def process_exception(self, request, exception):
print('应用异常了')
return JsonResponse({
'code':4000,
'msg':'非正常请求'
})