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

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的写法的更多相关文章

  1. Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)

    Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...

  2. Django 2.0官方文档中文 总索引

    Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本:  ...

  3. Django 从0开始创建一个项目

    title: Django 从0开始创建一个项目 tags: Django --- Django 从0开始创建一个项目 创建Django工程及配置 创建工程:django-admin starproj ...

  4. django 2.0 xadmin 错误集锦

    转载 django 2.0 xadmin 错误集锦 2018-03-26 10:39:18 Snail0Li 阅读数 5188更多 分类专栏: python   1.django2.0把from dj ...

  5. Django分析之Middleware中间件

    写了几周的脚本,今天终于开始接触web框架了~学习Python的web框架,那么Django就几乎是必修课了,这次的工作是先打打下手,主要的任务是在setting中添加版本号,在渲染静态css,js的 ...

  6. Django 2.0 新特性 抢先看!

    一.Python兼容性 Django 2.0支持Python3.4.3.5和3.6.Django官方强烈推荐每个系列的最新版本. 最重要的是Django 2.0不再支持Python2! Django ...

  7. Django 2.0 学习

    Django django是基于MTV结构的WEB框架 Model 数据库操作 Template 模版文件 View 业务处理 在Python中安装django 2.0 1 直接安装 pip inst ...

  8. Django组件之Middleware

    一.中间件 在django的settings.py文件下,有一个变量为MIDDLEWARE,里面放的就是中间件. MIDDLEWARE = [ 'django.middleware.security. ...

  9. Django 2.0.3 使用笔记

    运行环境: Python 3.5.2 Django 2.0.3 Django Admin中model显示为中文 定义model时,定义一个Meta对象,设置需要显示的中文名称.verbose_name ...

随机推荐

  1. contourf和contour用法区别

    import numpy as np import matplotlib.pyplot as plt %matplotlib inline from matplotlib.colors import ...

  2. 从匿名函数(闭包特性)到 PHP 设计模式之容器模式

    匿名函数(匿名函数) 匿名函数,也叫闭包函数,它允许临时创建一个没有指定名称的函数,常用作回调函数参数的值,也可以作为变量的值来使用.具体的使用见以下示例代码: /* 示例一:声明一个简单匿名函数,并 ...

  3. 工控安全入门之Modbus(转载)

    工控安全这个领域比较封闭,公开的资料很少.我在读<Hacking Exposed Industrial Control Systems>,一本16年的书,选了的部分章节进行翻译,以其抛砖引 ...

  4. springboot使用elasticsearch报No property index found for type错误

    一.前提:项目之前使用springboot+spring-data-mongodb.现在需要加入elasticsearch做搜索引擎,这样mongo和elasticsearch共存了. 二.报错信息: ...

  5. luogu P1486 [NOI2004]郁闷的出纳员

    一万年以后终于调过了这题 这道题主要是维护一个有序的集合(吧),所以使用平衡树(我这里用\(Splay\)) 记录一个变量\(ff\)(雾),表示所有工资的变化量 对于\(I\)操作,如果初始工资大于 ...

  6. 【python】pip安装报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 7: ordinal not in range(128)

    刚安装完python,准备pip安装第三方库的时候出现了一个错误: UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef in positio ...

  7. Java InputStream 、 InputStreamReader和BufferedReader

    https://blog.csdn.net/zgljl2012/article/details/47267609 在Java中,上述三个类经常用于处理数据流,下面介绍一下三个类的不同之处以及各自的用法 ...

  8. qt无法使用终端启动的解决方法

    在Terminal中直接输入命令就能打开QtCreator, i.e. ~$ qtcreator 就可以打开Qt Creator了. 想完成这个功能的原因是,一般在Linux下打命令比较方便,而师兄给 ...

  9. /etc/my.cnf

    [client] default-character-set=utf8 [mysqld] tmp_table_size = 2048M max_heap_table_size = 2048M max_ ...

  10. gdb revert, Go to previous line in gdb

    Yes! With the new version 7.0 gdb, you can do exactly that! The command would be "reverse-step& ...