码迷,mamicode.com
首页 > 其他好文 > 详细

Django 2.0 Middleware的写法

时间:2019-01-03 15:40:39      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:comm   style   with   pre   sage   res   request   util   exec   

网上很多写法,都是传统的写法,

process_request和process_response方法,还可以用,
但process_view的执行流程已经不行了。

看了官方文档,推荐的写法,也是用__call__方法来作实现了

我试了新老方法,从输出,可以看出效果了。

中间件处理的顺序还是request从上到下,response从下回到上的。

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


class Row1(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def process_request(self, request):
        print(中间件1的请求)

    def process_response(self, request, response):
        print(中间件1的返回)
        return response

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        print(中间件1的 view前调用)
        response = self.get_response(request)

        # Code to be executed for each request before
        # the view (and later middleware) are called.

        print(中间件1的 view之后调用)

        return response


class Row2(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def process_request(self, request):
        print(中间件2的请求)
        # return HttpResponse(‘前端显示:中间件:M2.process_request‘)

    def process_response(self, request, response):
        print(中间件2的返回)
        return response

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        print(中间件2的 view前调用)
        response = self.get_response(request)

        # Code to be executed for each request before
        # the view (and later middleware) are called.

        print(中间件2的 view之后调用)

        return response


class Row3(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def process_request(self, request):
        print(中间件3的请求)

    def process_response(self, request, response):
        print(中间件3的返回)
        return response

    def process_view(self, request, callback, callback_args, callback_kwargs):
        print(中间件3的 view)

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,
    Middle.cm1.Row1,
    Middle.cm1.Row2,
    Middle.cm1.Row3,
]

输出,注意Row3里process_view输出没有反应,

而Row1和Row2的process_request, process_rewponse的输出被忽略。

Quit the server with CTRL-BREAK.
中间件1的 view前调用
中间件2的 view前调用
中间件3的请求
中间件3的返回
中间件2的 view之后调用
中间件1的 view之后调用
[03/Jan/2019 20:08:58] "GET / HTTP/1.1" 200 16348

 

Django 2.0 Middleware的写法

标签:comm   style   with   pre   sage   res   request   util   exec   

原文地址:https://www.cnblogs.com/aguncn/p/10214364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!