middleware
Posted dragonbird
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了middleware相关的知识,希望对你有一定的参考价值。
自定义middleware
新建一个文件夹,里面写自己的middleware
在settings 设置,
在middleware.py文件中导入
from django.utils.deprecation import MiddlewareMixin
如果没法导入
可以这么写
class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_response super(MiddlewareMixin, self).__init__() def __call__(self, request): response = None if hasattr(self, \'process_request\'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self, \'process_response\'): response = self.process_response(request, response) return response
以上这段代码用到了责任连模式和类似递归的思想
自己写个代码模仿django 工作机制
class Md(object): def __init__(self,get_response=None): self.get_response = get_response def __call__(self,request): if hasattr(self,\'process_request\'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self,\'process_response\'): response = self.process_response(request,response) return response class Md1(Md): def process_request(self,request): print(\'request>>>md1\') def process_response(self,request,response): response.name=\'yuyang\' print(\'response<<<<<md1\') return response class Md2(Md): def process_request(self,request): print(\'request>>>md2\') # return Response() def process_response(self,request,response): response.age=\'12\' print(\'response<<<<<md2\') return response class Md3(Md): def process_request(self,request): print(\'request>>>md3\') def process_response(self,request,response): response.gender=\'male\' print(\'response<<<<<md3\') return response class Request(object): def __init__(self): self.name = \'request\' request_obj = Request() class Response(object): def __init__(self): self.body=\'hello world\' pass # 实例化 def view(request): print(\'>>>>>>>>view function <<<<<<<<<<\') return Response() md3_obj = Md3(view) md2_obj = Md2(md3_obj) md1_obj = Md1(md2_obj) res = md1_obj(request_obj) print(res.__dict__)
class M1(MiddlewareMixin): def process_request(self,request): print(\'m1.process_request\') def process_response(self,request,response): print(\'m1.process_response\') return response def process_view(self, request, view_func, view_func_args, view_func_kwargs): print(\'m1.process_view\') return render(request, \'login.html\') class M2(MiddlewareMixin): def process_request(self, request): print(\'m2.process_request\') def process_view(self, request, view_func, view_func_args, view_func_kwargs): print(\'M2.process_view\') # return render(request, \'login.html\') def process_exception(self, request, exception): print(\'m2.process_exception\') return HttpResponse(\'开发的程序员已经被打死\') def process_response(self, request, response): print(\'m2.process_response\') return response def process_template_response(self, request, response): print(\'m2.process_template_response\') return response
5个方法:
process_request
process_response
process_exception
process_template_respons
process_view(self, request, view_func, view_func_args, view_func_kwargs)
process_response 是要有返回值的,
关于执行的顺序,
在上面这个列表中,当http请求到来,request会依次经过app01.md.M1,......django.middleware.clickjacking.XFrameOptionsMiddleware
response 正好相反
note:
以上是关于middleware的主要内容,如果未能解决你的问题,请参考以下文章
使用 http-proxy-middleware 处理 WebSocket 错误