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

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. IDEA不生成WAR包,报错

    com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionExceptionProjec ...

  2. asp.net mvc简单分页实例

    @{ ViewBag.Title = "Index"; } @{ int pageIndex = (int)ViewBag.CurrentPage; int pageCount = ...

  3. 字体选择框QFontComboBox

    self.combobox_2 = QFontComboBox(self)  # 实例化字体列表框 combobox.currentFont()  返回字体选择框中当前的字体 self.combobo ...

  4. ActiveMQ集成Spring使用

    现在任何一个框架的使用都会结合spring框架,quartz.cxf与平时常见的Hibernate.mybatis.Struts等都可以与spring集成起来使用,在这里研究了activemq结合sp ...

  5. shiro自定义realm认证(五)

    上一节介绍了realm的作用: realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null.token就相当于是对用户输入 ...

  6. MoveIt! 源安装

    rosdep update sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install python-wstool pytho ...

  7. 简单理解Zookeeper的Leader选举【转】

    Leader选举是保证分布式数据一致性的关键所在.Leader选举分为Zookeeper集群初始化启动时选举和Zookeeper集群运行期间Leader重新选举两种情况.在讲解Leader选举前先了解 ...

  8. mysql之 innobackupex备份+binlog日志的完全恢复【转】

    前言: MySQL的完全恢复,我们可以借助于完整的 备份+binlog 来将数据库恢复到故障点. 备份可以是热备与逻辑备份(mysqldump),只要备份与binlog是完整的,都可以实现完全恢复. ...

  9. C++ virtual函数重写,在继承的时候没有在函数前写virtual关键字也依然是虚函数吗?

    比如: class Base { Base() {}; ~Base() {}; virtual void Init(); }; class Derived:public Base { virtual ...

  10. QML C++插件dll引用

    插件的创建非常简单,但是它可以复用,并且为不同的应用程序扩展类型.使用创建的插件是非常灵活的解决方案.关于插件一个很好的例子见QmlBook-In-Chinese 中最后一章介绍的例子. 本文主要备忘 ...