Django 2.0 Middleware的写法

Posted aguncn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 2.0 Middleware的写法相关的知识,希望对你有一定的参考价值。

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

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的写法的主要内容,如果未能解决你的问题,请参考以下文章

Django之 CVB&FVB

django基础

Django组件之Middleware

Django中Middleware中间件

django_heroku.settings(locals()) KeyError: 'MIDDLEWARE' 和 'MIDDLEWARE_CLASSES'

利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击