django Middleware

一、中间件运作流程

1、自己的理解

client middleware1 middlewareN view request 1\process_request(从1到N) 2\process_view(从1到N) 通过url找到view process_exception 遇到异常process_exception(从N到1) 中途有处理直接回 如果有异常且pro cess_exce ption处理,带 上处理结果repo nse process_response process_response response client middleware1 middlewareN view

2、参考的图

参考

二、流程试例

process_request(self, request) process_view(self,request, callback,
call_args, call_kwargs) process_template_response(self, request,
response) process_exception(self, request, exception)
process_response(self, request, response)

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse

class MMM(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MMM, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            # 执行下一个__call__
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response




class MDW1(MMM):
    def process_request(self, request):
        print('mdw1-->process_request')
    def process_view(self, request, view_func, view_args, view_kwargs):
        print('mdw1-->process_view')

    def process_response(self, request, response):
        print('mdw1-->process_response')
        return response

    def process_exception(self, request, exception):
        print('mdw1-->process_exception')

    def process_template_response(self,request,response):
        print('mdw1--> process_template_response')
        return response

class MDW2(MMM):

    def process_request(self, request):
        print('mdw2-->process_request')

    def process_view(self, request, view_func, view_args, view_kwargs):
        print('mdw2-->process_view')

    def process_response(self, request, response):
        print('mdw2-->process_response')
        return response

    def process_exception(self, request, exception):
        print('mdw2-->process_exception')
        return HttpResponse('OK')

    def process_template_response(self,request,response):
        print('mdw2--> process_template_response')
        return response

class MDW3(MMM):
    def process_request(self, request):
        print('mdw3-->process_request')

    def process_view(self, request, view_func, view_args, view_kwargs):
        print('mdw3-->process_view')

    def process_response(self, request, response):
        print('mdw3-->process_response')
        return response

    def process_exception(self, request, exception):
        print('mdw3-->process_exception')
        # return HttpResponse('OK')
        # print('mdw3-->process_exception')
    def process_template_response(self,request,response):
        # print(request,type(request))
        # print(response,type(response))
        print('mdw3--> process_template_response')
        return response
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值