详解Django中间件的5种自定义方法


Posted in Python onJuly 26, 2018

Django中间件

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

中间件的执行流程

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

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

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

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

详解Django中间件的5种自定义方法

自定义中间件

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

详解Django中间件的5种自定义方法

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变量,其中每一个元素就是一个中间件

详解Django中间件的5种自定义方法

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',
]

执行结果

详解Django中间件的5种自定义方法

为啥报错了呢?

因为 自定义的中间件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 #执行完了这个中间件一定要 传递给下一个中间件

详解Django中间件的5种自定义方法

中间件(类)中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

执行结果

详解Django中间件的5种自定义方法

玩法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

执行结果

详解Django中间件的5种自定义方法

结论:

如果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默认不执行!!!

详解Django中间件的5种自定义方法

大爷的 原来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()默认不执行

详解Django中间件的5种自定义方法

rocess_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

执行结果

详解Django中间件的5种自定义方法

应用:

既然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吗?)

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python fileinput模块使用实例
May 28 Python
Python用list或dict字段模式读取文件的方法
Jan 10 Python
SQLite3中文编码 Python的实现
Jan 11 Python
python 计算两个日期相差多少个月实例代码
May 24 Python
Python列表解析配合if else的方法
Jun 23 Python
符合语言习惯的 Python 优雅编程技巧【推荐】
Sep 25 Python
python复制列表时[:]和[::]之间有什么区别
Oct 16 Python
python 实现将txt文件多行合并为一行并将中间的空格去掉方法
Dec 20 Python
python实现kmp算法的实例代码
Apr 03 Python
django API 中接口的互相调用实例
Apr 01 Python
基于python实现操作redis及消息队列
Aug 27 Python
Django中如何用xlwt生成表格的方法步骤
Jan 31 Python
python opencv实现切变换 不裁减图片
Jul 26 #Python
Flask之flask-script模块使用
Jul 26 #Python
对tf.reduce_sum tensorflow维度上的操作详解
Jul 26 #Python
TensorFlow用expand_dim()来增加维度的方法
Jul 26 #Python
Python迭代器与生成器基本用法分析
Jul 26 #Python
Python实现矩阵相乘的三种方法小结
Jul 26 #Python
python实现泊松图像融合
Jul 26 #Python
You might like
php实现获取文章内容第一张图片的方法
2014/11/04 PHP
Yii2.0实现的批量更新及批量插入功能示例
2019/01/29 PHP
Ajax+PHP实现的模拟进度条功能示例
2019/02/11 PHP
js wmp操作代码小结(音乐连播功能)
2008/11/08 Javascript
jQuery实现的多选框多级联动插件
2014/05/02 Javascript
单击某一段文字改写文本颜色
2014/06/06 Javascript
node.js不得不说的12点内容
2014/07/14 Javascript
js实现仿QQ秀换装效果的方法
2015/03/04 Javascript
jquery实现滑动特效代码
2015/08/10 Javascript
JS+CSS实现DIV层的展开、收缩效果
2016/01/28 Javascript
JavaScript实现简单的拖动效果
2016/07/02 Javascript
使用JavaScript判断手机浏览器是横屏还是竖屏问题
2016/08/02 Javascript
关于js函数解释(包括内嵌,对象等)
2016/11/20 Javascript
微信开发 JS-SDK 6.0.2 经常遇到问题总结
2016/12/08 Javascript
Javascript网页抢红包外挂实现分享
2018/01/11 Javascript
Vue中强制组件重新渲染的正确方法
2021/01/03 Vue.js
[03:46]DOTA2英雄基础教程 维萨吉
2013/12/11 DOTA
[46:47]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS NE
2014/05/22 DOTA
python数据结构之二叉树的建立实例
2014/04/29 Python
Python入门篇之文件
2014/10/20 Python
在Python中使用列表生成式的教程
2015/04/27 Python
在Python中使用__slots__方法的详细教程
2015/04/28 Python
python pandas dataframe 按列或者按行合并的方法
2018/04/12 Python
分厂厂长岗位职责
2013/12/29 职场文书
初一生物教学反思
2014/01/18 职场文书
《会变的花树叶》教学反思
2014/02/10 职场文书
行政办公室岗位职责
2014/03/18 职场文书
网站创业计划书
2014/04/30 职场文书
企业文化标语大全
2014/06/10 职场文书
住房租房协议书
2014/08/20 职场文书
防灾减灾活动总结
2014/08/30 职场文书
2014年幼儿园教研工作总结
2014/12/04 职场文书
关于颐和园的导游词
2015/01/30 职场文书
管理失职检讨书范文
2015/05/05 职场文书
催款函范文
2015/06/24 职场文书
护士年终工作总结不会写?各科护士模板总结
2020/01/02 职场文书