Django中间件

在http请求 到达视图函数之前   和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法。

Django1.9版本以后中间件的执行流程

1、执行完所有的request方法 到达视图函数。

2、执行中间件的其他方法

3、经过所有response方法 返回客户端。

注意:如果在其中1个中间件里 request方法里 return了值,就会执行当前中间件的response方法,返回给用户 然后 报错。。不会再执行下一个中间件。

 Django 1.9版本之前,如果在request方法中遇到return,会执行最后一个中间件的response方法,然后依次回传

自定义中间件

1.在project下随便创建一个py文件

from django.utils.deprecation import MiddlewareMixin
class Middle1(MiddlewareMixin):
def process_request(self,request):
print("来了")
def process_response(self, request,response):
print('走了')

2、在setings文件中 注册这个 py文件

django项目的settings模块中,有一个 MIDDLEWARE_CLASSES 变量,其中每一个元素就是一个中间件

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',
'M1.Middle1',
]

 执行结果

为啥报错了呢?

因为 自定义的中间件response方法没有return,交给下一个中间件,导致http请求中断了!!!

注意 自定义的中间件request 方法不要return  因为返回值中间件不再往下执行,导致 http请求到达不了视图层,因为request在视图之前执行!

from django.utils.deprecation import MiddlewareMixin
class Middle1(MiddlewareMixin):
def process_request(self,request):
print("来了") #不用return Django内部自动帮我们传递
def process_response(self, request,response):
print('走了')
return response #执行完了这个中间件一定要 传递给下一个中间件

 

中间件(类)中5种方法

中间件中可以定义5个方法,分别是:

  • process_request(self,request)
  • process_view(self, request, callback, callback_args, callback_kwargs)
  • process_template_response(self,request,response)
  • process_exception(self, request, exception)
  • process_response(self, request, response

1、 process_view(self, request, callback, callback_args, callback_kwargs)方法介绍

(1)执行完所有中间件的request方法‘

(2)url匹配成功

(3)拿到 视图函数的名称、参数,(注意不执行) 再执行process_view()方法

(4)最后去执行视图函数

玩法1(常规)

from  django.utils.deprecation import MiddlewareMixin

class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view") def process_response(self, request, response):
print('M1.response')
return response class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view") def process_response(self, request, response):
print('M2.response')
return response

执行结果

玩法2

既然 process_view 拿到视图函数的名称、参数,(不执行) 再执行process_view()方法,最后才去执行视图函数!

那可以在 执行process_view环节直接 把函数执行返回吗?

from  django.utils.deprecation import MiddlewareMixin

class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request')
# callback视图函数名称 callback_args,callback_kwargs 视图函数执行所需的参数
def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view")
response=callback(request,*callback_args,**callback_kwargs)
return response
def process_response(self, request, response):
print('M1.response')
return response class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view")
def process_response(self, request, response):
print('M2.response')
return response

执行结果

结论:

如果process_view函数有返回值,跳转到最后一个中间件, 执行最后一个中间件的response方法,逐步返回。

和 process_request方法不一样哦!  request方法在当前中间件的response方法返回。

2、process_exception(self, request, exception)方法

from  django.utils.deprecation import MiddlewareMixin

class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view") def process_response(self, request, response):
print('M1.response')
return response def process_exception(self, request,exception):
print('M1的process_exception') class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view") def process_response(self, request, response):
print('M2.response')
return response def process_exception(self, request, exception):
print('M2的process_exception')

我去 加了process_exception方法 咋啥也没执行呢?!!原来是process_exception默认不执行!!!

大爷的 原来process_exception方法在 视图函数执行出错的时候才会执行

M1.request
M2.request
M1.process_view
M2.process_view
执行index
M2的process_exception
M1的process_exception
Internal Server Error: /index/
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\untitled1\app01\views.py", line 7, in index
int("ok")
ValueError: invalid literal for int() with base 10: 'ok'
M2.response
M1.response
[03/Jul/2017 16:43:59] "GET /index/ HTTP/1.1" 500 62663

1、执行完所有 request 方法

2、执行 所有 process_view方法

3、如果视图函数出错,执行process_exception(最终response,process_exception的return值)

如果process_exception 方法有了 返回值 就不再执行 其他中间件的 process_exception,直接执行response方法响应

4.执行所有response方法

5.最后返回process_exception的返回值

M1.request
M2.request
M1.process_view
M2.process_view
执行index
M2的process_exception (有了return值,直接执行response)
M2.response
M1.response

process_exception的应用

在视图函数执行出错时,返回错误信息。这样页面就不会 报错了!

class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view") def process_response(self, request, response):
print('M1.response')
return response def process_exception(self, request,exception):
print('M1的process_exception') class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view") def process_response(self, request, response):
print('M2.response')
return response def process_exception(self, request, exception):
print('M2的process_exception')
return HttpResponse('出错了兄弟!!!')

3、process_template_response()

from  django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view") def process_response(self, request, response):
print('M1.response')
return response def process_exception(self, request,exception):
print('M1的process_exception') class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view") def process_response(self, request, response):
print('M2.response')
return response def process_exception(self, request, exception):
print('M2的process_exception') def process_template_response(self,request,response):
print('M2process_template_response')
return response

process_template_response()默认不执行

 process_template_response()特性

只有在视图函数的返回对象中有render方法才会执行!

并把对象的render方法的返回值返回给用户(注意不返回视图函数的return的结果了,而是返回视图函数 return值(对象)的render方法)

from  django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse class M1(MiddlewareMixin):
def process_request(self, request):
print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M1.process_view") def process_response(self, request, response):
print('M1.response')
return response def process_exception(self, request,exception):
print('M1的process_exception') class M2(MiddlewareMixin):
def process_request(self, request):
print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ):
print("M2.process_view") def process_response(self, request, response):
print('M2.response')
return response def process_exception(self, request, exception):
print('M2的process_exception') def process_template_response(self,request,response): #如果视图函数中的返回值 中有render方法,才会执行 process_template_response
print('M2process_template_response')
return response

视图函数

from django.shortcuts import render,HttpResponse

# Create your views here.
class Foo():
def __init__(self,requ):
self.req=requ
def render(self):
return HttpResponse('OKKKK') def index(request):
print("执行index")
obj=Foo(request)
return obj

执行结果

 应用:

既然process_template_respnse,不返回视图函数的return的结果,而是返回视图函数 return值(对象)的render方法;(多加了一个环节)

就可以在 这个视图函数返回对象的 render方法里,做返回值的二次加工了!多加工几个,视图函数就可以随便使用了!

(好比 喷雾器有了多个喷头,换不同的喷头喷出不同水,返回值就可以也组件化了)

from django.shortcuts import render,HttpResponse

# Create your views here.
class Dict(): #对视图函数返回值做二次封装 !!
def __init__(self,requ,msg):
self.req=requ
self.msg=msg
def render(self):
a=self.msg #在render方法里面 把视图函数的 返回值 制作成字典 、列表等。。。
# 如果新增了其他 一个视图函数直接,return对象 即可!不用每个视图函数都写 制作字典 列表 拼接的逻辑了
return HttpResponse(a) # def index(request):
print("执行index")
obj=Dict(request,"vv")
return obj

中间件应用场景

由于中间件工作在 视图函数执行前、执行后(像不像所有视图函数的装饰器!)适合所有的请求/一部分请求做批量处理

1、做IP限制

放在 中间件类的列表中,阻止某些IP访问了;

2、URL访问过滤

如果用户访问的是login视图(放过)

如果访问其他视图(需要检测是不是有session已经有了放行,没有返回login),这样就省得在 多个视图函数上写装饰器了!

3、缓存(还记得CDN吗?)

客户端请求来了,中间件去缓存看看有没有数据,有直接返回给用户,没有再去逻辑层 执行视图函数

Django----中间件详解的更多相关文章

  1. Django 2.0 学习(20):Django 中间件详解

    Django 中间件详解 Django中间件 在Django中,中间件(middleware)其实就是一个类,在请求到来和结束后,Django会根据自己的规则在合适的时机执行中间件中相应的方法. 1. ...

  2. Django中间件详解

    Django中间件详解 中间件位置 WSGI 主要负责的就是负责和浏览器和应用之家沟通的桥梁 浏览器发送过来一个http请求,WSGI负责解包,并封装成能够给APP使用的environ,当app数据返 ...

  3. Django -- settings 详解

    Django settings详解 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包含到python可以找到的目录下,开发情况下不需要,我们通常会在当前文件夹运 ...

  4. Django -- settings 详解(转)

    Django -- settings 详解   Django settings详解 1.基础 DJANGO_SETTING_MODULE环境变量:让settings模块被包含到python可以找到的目 ...

  5. net core 中间件详解及项目实战

    net core 中间件详解及项目实战 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的H ...

  6. Django框架详解之url

    Django基本命令 下载Django pip3 install django 创建一个django project django-admin.py startproject cms 当前目录下会生成 ...

  7. 中间件详解,Django复习

    day74 2018-05-21 课程安排周一: 中间件 auth模块+分析BBS项目需求(小组讨论把表结构设计出来) 1. 今日内容 中间件:http://www.cnblogs.com/liwen ...

  8. Django路由详解

    一.路由基础 1.路由url函数:路由自上而下进行匹配:url(正则路径,视图函数内存地址,默认参数,路由别名) 2.路由正则: 规定开始:^ | 规定结束:$ #url(r'index', view ...

  9. Django session 详解-part II-session

    Django中的session是一个高级工具,它可以让用户存储个人信息以便在下次访问网站中使用这些信息.session的基础还是cookie,但是它提供了一些更加高级的功能.请看下面的一个例子: 使用 ...

  10. 很详细的Django入门详解

    Django 是用Python开发的一个免费开源的Web框架,可以用于快速搭建高性能,优雅的网站!采用了MVC的框架模式,即模型M,视图V和控制器C,也可以称为MVT模式,模型M,视图V,模板T.在学 ...

随机推荐

  1. 点聚合功能---基于ARCGIS RUNTIME SDK FOR ANDROID

    一直不更新博客的原因,如果一定要找一个,那就是忙,或者说懒癌犯了. 基于ArcGIS RunTime SDK for Android的点聚合功能,本来是我之前做过的一个系统里面的一个小部分,今天抽出一 ...

  2. 广州图书馆借阅抓取——httpClient的使用

    欢迎访问我的个人网站,要是能在GitHub上对网站源码给个star就更好了. 搭建自己的网站的时候,想把自己读过借过的书都想记录一下,大学也做过自己学校的借书记录的爬取,但是数据库删掉了==,只保留一 ...

  3. Javascript从“繁”到“简”进行数组去重

    随着JavaScript提供语法的增多,数组去重方式也越来越多.现在从最原始的方式到最简洁的方式,一步步进行剖析. 双重循环 数组去重,不就是比较数组元素,去掉重复出现的么.最原始的方式不正是双重循环 ...

  4. iOS自动化环境搭建——macaca

    macaca-java for ios 自动化环境搭建 基础原理解析:https://testerhome.com/topics/6608 一.环境搭建 1.安装eclipse; -----Java开 ...

  5. JAVA编程入门

    java最早是由Sun公司基于C++开发而成的新一代编程语言也是现行下的主流行编程语言,其原始的主要用于嵌入式开发.java的第一个版本为JDK1.0,到2017年已经升级到JAK1.9版本.java ...

  6. 【JAVA零基础入门系列】Day9 Java中的那个大数值

    什么是大数值?用脚趾头想也知道,当然是"大"的数值(233).Java中有两个用于表示大数值的类,BigInteger和BigDecimal,那到底能表示多大的数值呢?理论上,可以 ...

  7. 【NOIP2016 Day1 T1】玩具谜题

    原题:https://www.luogu.org/problemnew/show/P1563 题目大意:有N个人围成一个圈,给定一串未化简的物品移动关系,要求你通过这些未化简的关系以及起始段的编号,求 ...

  8. 【ASP.NET MVC 学习笔记】- 08 URL Routing

    本文参考:http://www.cnblogs.com/willick/p/3343105.html 1.URL Routing告诉MVC如何正确的定位Controller和Action. 2.URL ...

  9. LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 顺序线性表 ---- ArrayList 源码解析及实现原理分析

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7738888.html ------------------------------------ ...