Django框架 之 中间件
Django框架 之 中间件
浏览目录
中间件介绍
自定义中间件
中间件的执行流程
中间件版登录验证
一、中间件介绍
官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子。它是一个轻量、低级别的插件系统,用于在全局范围内改变Django的输入和输出。每个中间件组件都负责做一些特定的功能。
说的直白一点中间件是帮助我们在视图函数执行之前和执行之后都可以做一些额外的操作,它本质上就是一个自定义类,类中定义了几个方法,Django框架会在请求的特定的时间去执行这些方法。
我们一直都在使用中间件,只是没有注意到而已,打开Django项目的Settings.py文件,看到下面的MIDDLEWARE配置项。
1
2
3
4
5
6
7
8
9
|
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' ,
]
|
MIDDLEWARE配置项是一个列表,列表中是一个个字符串,这些字符串其实是一个个类,也就是一个个中间件。
我们之前已经接触过一个csrf相关的中间件了?我们一开始让大家把他注释掉,再提交post请求的时候,就不会被forbidden了,后来学会使用csrf_token之后就不再注释这个中间件了。
二、自定义中间件
中间件可以定义五个方法,分别是:(主要的是process_request和process_response)
- process_request(self,request)
- process_view(self, request, view_func, view_args, view_kwargs)
- process_template_response(self,request,response)
- process_exception(self, request, exception)
- process_response(self, request, response)
以上方法的返回值可以是None或一个HttpResponse对象,如果是None,则继续按照django定义的规则向后继续执行,如果是HttpResponse对象,则直接将该对象返回给用户。
自定义一个中间件示例
1
2
3
4
5
6
7
8
9
10
11
|
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
|
process_request
process_request有一个参数,就是request,这个request和视图函数中的request是一样的。
它的返回值可以是None也可以是HttpResponse对象。返回值是None的话,按正常流程继续走,交给下一个中间件处理,如果是HttpResponse对象,Django将不执行视图函数,而将相应对象返回给浏览器。
我们来看看多个中间件时,Django是如何执行其中的process_request方法的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
class MD2(MiddlewareMixin):
def process_request( self , request):
print ( "MD2里面的 process_request" )
pass
|
在settings.py的MIDDLEWARE配置项中注册上述两个自定义中间件:
1
2
3
4
5
6
7
8
9
10
11
|
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' ,
'middlewares.MD1' , # 自定义中间件MD1
'middlewares.MD2' # 自定义中间件MD2
]
|
此时,我们访问一个视图,会发现终端中打印如下内容:
1
2
3
|
MD1里面的 process_request
MD2里面的 process_request
app01 中的 index视图
|
把MD1和MD2的位置调换一下,再访问一个视图,会发现终端中打印的内容如下:
1
2
3
|
MD2里面的 process_request
MD1里面的 process_request
app01 中的 index视图
|
看结果我们知道:视图函数还是最后执行的,MD2比MD1先执行自己的process_request方法。
在打印一下两个自定义中间件中process_request方法中的request参数,会发现它们是同一个对象。
由此总结一下:
- 中间件的process_request方法是在执行视图函数之前执行的。
- 当配置多个中间件时,会按照MIDDLEWARE中的注册顺序,也就是列表的索引值,从前到后依次执行的。
- 不同中间件之间传递的request都是同一个对象
多个中间件中的process_response方法是按照MIDDLEWARE中的注册顺序倒序执行的,也就是说第一个中间件的process_request方法首先执行,而它的process_response方法最后执行,最后一个中间件的process_request方法最后一个执行,它的process_response方法是最先执行。
process_response
它有两个参数,一个是request,一个是response,request就是上述例子中一样的对象,response是视图函数返回的HttpResponse对象。该方法的返回值也必须是HttpResponse对象。
给上述的M1和M2加上process_response方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
class MD2(MiddlewareMixin):
def process_request( self , request):
print ( "MD2里面的 process_request" )
pass
def process_response( self , request, response):
print ( "MD2里面的 process_response" )
return response
|
访问一个视图,看一下终端的输出:
1
2
3
4
5
|
MD2里面的 process_request
MD1里面的 process_request
app01 中的 index视图
MD1里面的 process_response
MD2里面的 process_response
|
看结果可知:
process_response方法是在视图函数之后执行的,并且顺序是MD1比MD2先执行。(此时settings.py中 MD2比MD1先注册)
多个中间件中的process_response方法是按照MIDDLEWARE中的注册顺序倒序执行的,也就是说第一个中间件的process_request方法首先执行,而它的process_response方法最后执行,最后一个中间件的process_request方法最后一个执行,它的process_response方法是最先执行。
process_view
process_view(self, request, view_func, view_args, view_kwargs)
该方法有四个参数
request是HttpRequest对象。
view_func是Django即将使用的视图函数。 (它是实际的函数对象,而不是函数的名称作为字符串。)
view_args是将传递给视图的位置参数的列表.
view_kwargs是将传递给视图的关键字参数的字典。 view_args和view_kwargs都不包含第一个视图参数(request)。
Django会在调用视图函数之前调用process_view方法。
它应该返回None或一个HttpResponse对象。 如果返回None,Django将继续处理这个请求,执行任何其他中间件的process_view方法,然后在执行相应的视图。 如果它返回一个HttpResponse对象,Django不会调用适当的视图函数。 它将执行中间件的process_response方法并将应用到该HttpResponse并返回结果。
给MD1和MD2添加process_view方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD1 中的process_view" )
print (view_func, view_func.__name__)
class MD2(MiddlewareMixin):
def process_request( self , request):
print ( "MD2里面的 process_request" )
pass
def process_response( self , request, response):
print ( "MD2里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD2 中的process_view" )
print (view_func, view_func.__name__)
|
访问index视图函数,看一下输出结果:
1
2
3
4
5
6
7
8
9
10
11
|
MD2里面的 process_request
MD1里面的 process_request
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD2 中的process_view
<function index at 0x000001DE68317488 > index
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD1 中的process_view
<function index at 0x000001DE68317488 > index
app01 中的 index视图
MD1里面的 process_response
MD2里面的 process_response
|
process_view方法是在process_request之后,视图函数之前执行的,执行顺序按照MIDDLEWARE中的注册顺序从前到后顺序执行的.
process_exception
process_exception(self, request, exception)
该方法两个参数:
一个HttpRequest对象
一个exception是视图函数异常产生的Exception对象。
这个方法只有在视图函数中出现异常了才执行,它返回的值可以是一个None也可以是一个HttpResponse对象。如果是HttpResponse对象,Django将调用模板和中间件中的process_response方法,并返回给浏览器,否则将默认处理异常。如果返回一个None,则交给下一个中间件的process_exception方法来处理异常。它的执行顺序也是按照中间件注册顺序的倒序执行。
给MD1和MD2添加上这个方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD1 中的process_view" )
print (view_func, view_func.__name__)
def process_exception( self , request, exception):
print (exception)
print ( "MD1 中的process_exception" )
class MD2(MiddlewareMixin):
def process_request( self , request):
print ( "MD2里面的 process_request" )
pass
def process_response( self , request, response):
print ( "MD2里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD2 中的process_view" )
print (view_func, view_func.__name__)
def process_exception( self , request, exception):
print (exception)
print ( "MD2 中的process_exception" )
|
如果视图函数中无异常,process_exception方法不执行。
想办法,在视图函数中抛出一个异常:
1
2
3
4
|
def index(request):
print ( "app01 中的 index视图" )
raise ValueError( "呵呵" )
return HttpResponse( "O98K" )
|
在MD1的process_exception中返回一个响应对象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD1 中的process_view" )
print (view_func, view_func.__name__)
def process_exception( self , request, exception):
print (exception)
print ( "MD1 中的process_exception" )
return HttpResponse( str (exception)) # 返回一个响应对象
|
看输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
MD2里面的 process_request
MD1里面的 process_request
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD2 中的process_view
<function index at 0x0000022C09727488 > index
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD1 中的process_view
<function index at 0x0000022C09727488 > index
app01 中的 index视图
呵呵
MD1 中的process_exception
MD1里面的 process_response
MD2里面的 process_response
|
注意,这里并没有执行MD2的process_exception方法,因为MD1中的process_exception方法直接返回了一个响应对象。
process_template_response(了解)
process_template_response(self, request, response)
它的参数,一个HttpRequest对象,response是TemplateResponse对象(由视图函数或者中间件产生)。
process_template_response是在视图函数执行完成后立即执行,但是它有一个前提条件,那就是视图函数返回的对象有一个render()方法(或者表明该对象是一个TemplateResponse对象或等价方法)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class MD1(MiddlewareMixin):
def process_request( self , request):
print ( "MD1里面的 process_request" )
def process_response( self , request, response):
print ( "MD1里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD1 中的process_view" )
print (view_func, view_func.__name__)
def process_exception( self , request, exception):
print (exception)
print ( "MD1 中的process_exception" )
return HttpResponse( str (exception))
def process_template_response( self , request, response):
print ( "MD1 中的process_template_response" )
return response
class MD2(MiddlewareMixin):
def process_request( self , request):
print ( "MD2里面的 process_request" )
pass
def process_response( self , request, response):
print ( "MD2里面的 process_response" )
return response
def process_view( self , request, view_func, view_args, view_kwargs):
print ( "-" * 80 )
print ( "MD2 中的process_view" )
print (view_func, view_func.__name__)
def process_exception( self , request, exception):
print (exception)
print ( "MD2 中的process_exception" )
def process_template_response( self , request, response):
print ( "MD2 中的process_template_response" )
return response
|
views.py中:
1
2
3
4
5
6
7
8
9
|
def index(request):
print ( "app01 中的 index视图" )
def render():
print ( "in index/render" )
return HttpResponse( "O98K" )
rep = HttpResponse( "OK" )
rep.render = render
return rep
|
访问index视图,终端输出的结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
MD2里面的 process_request
MD1里面的 process_request
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD2 中的process_view
<function index at 0x000001C111B97488 > index
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MD1 中的process_view
<function index at 0x000001C111B97488 > index
app01 中的 index视图
MD1 中的process_template_response
MD2 中的process_template_response
in index / render
MD1里面的 process_response
MD2里面的 process_response
|
从结果看出:
视图函数执行完之后,立即执行了中间件的process_template_response方法,顺序是倒序,先执行MD1的,在执行MD2的,接着执行了视图函数返回的HttpResponse对象的render方法,返回了一个新的HttpResponse对象,接着执行中间件的process_response方法。
小总结
三、中间件的执行流程
上一部分,我们了解了中间件中的5个方法,它们的参数、返回值以及什么时候执行,现在总结一下中间件的执行流程。
请求到达中间件之后,先按照正序执行每个注册中间件的process_reques方法,process_request方法返回的值是None,就依次执行,如果返回的值是HttpResponse对象,不再执行后面的process_request方法,而是执行当前对应中间件的process_response方法,将HttpResponse对象返回给浏览器。也就是说:如果MIDDLEWARE中注册了6个中间件,执行过程中,第3个中间件返回了一个HttpResponse对象,那么第4,5,6中间件的process_request和process_response方法都不执行,顺序执行3,2,1中间件的process_response方法。
process_request方法都执行完后,匹配路由,找到要执行的视图函数,先不执行视图函数,先执行中间件中的process_view方法,process_view方法返回None,继续按顺序执行,所有process_view方法执行完后执行视图函数。加入中间件3 的process_view方法返回了HttpResponse对象,则4,5,6的process_view以及视图函数都不执行,直接从最后一个中间件,也就是中间件6的process_response方法开始倒序执行。
process_template_response和process_exception两个方法的触发是有条件的,执行顺序也是倒序。总结所有的执行流程如下:
四、中间件版登录验证
中间件版的登录验证需要依靠session,所以数据库中要有django_session表。
![](/assets/blank.gif)
![](/assets/blank.gif)
from django.conf.urls import url from app01 import viewsurlpatterns = [url(r'^index/$', views.index),url(r'^login/$', views.login, name='login'), ]
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
from django.shortcuts import render, HttpResponse, redirectdef index(request):return HttpResponse('this is index')def home(request):return HttpResponse('this is home')def login(request):if request.method == "POST":user = request.POST.get("user")pwd = request.POST.get("pwd")if user == "Q1mi" and pwd == "123456":# 设置sessionrequest.session["user"] = user# 获取跳到登陆页面之前的URLnext_url = request.GET.get("next")# 如果有,就跳转回登陆之前的URLif next_url:return redirect(next_url)# 否则默认跳转到index页面else:return redirect("/index/")return render(request, "login.html")
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>登录页面</title> </head> <body> <form action="{% url 'login' %}"><p><label for="user">用户名:</label><input type="text" name="user" id="user"></p><p><label for="pwd">密 码:</label><input type="text" name="pwd" id="pwd"></p><input type="submit" value="登录"> </form> </body> </html>
![](/assets/blank.gif)
middlewares.py
![](/assets/blank.gif)
![](/assets/blank.gif)
class AuthMD(MiddlewareMixin):white_list = ['/login/', ] # 白名单balck_list = ['/black/', ] # 黑名单def process_request(self, request):from django.shortcuts import redirect, HttpResponsenext_url = request.path_infoprint(request.path_info, request.get_full_path())if next_url in self.white_list or request.session.get("user"):returnelif next_url in self.balck_list:return HttpResponse('This is an illegal URL')else:return redirect("/login/?next={}".format(next_url))
![](/assets/blank.gif)
在settings.py中注册
![](/assets/blank.gif)
![](/assets/blank.gif)
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','middlewares.AuthMD', ]
![](/assets/blank.gif)
AuthMD中间件注册后,所有的请求都要走AuthMD的process_request方法。
访问的URL在白名单内或者session中有user用户名,则不做阻拦走正常流程;
如果URL在黑名单中,则返回This is an illegal URL的字符串;
正常的URL但是需要登录后访问,让浏览器跳转到登录页面。
注:AuthMD中间件中需要session,所以AuthMD注册的位置要在session中间的下方。
附:Django请求流程图
转载于:https://www.cnblogs.com/hanbowen/p/9567077.html
Django框架 之 中间件相关推荐
- django框架之中间件 Auth模块
CBV加装饰器 方式一:装饰器加到想装饰的方法上 方式二:装饰器加到class前面,通过name参数指定被装饰的方法 方式三:重写dispatch(django分发CBV视图函数),直接给dispat ...
- python写数据库中间件_python3开发进阶-Django框架的中间件的五种用法和逻辑过程...
阅读目录 一.什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出. 每个中间件组件都 ...
- django框架之中间件
内容回顾: 内容回顾:https://www.cnblogs.com/liwenzhou/p/8343243.html 1. Cookie和Session 1. Cookie是 ...
- django python3会员中心_python3开发进阶-Django框架的自带认证功能auth模块和User对象的基本操作...
阅读目录 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: authenticate( ...
- Python 之 Django框架( Cookie和Session、Django中间件、AJAX、Django序列化)
12.4 Cookie和Session 12.41 cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务 ...
- Django 框架篇(七) : 中间件 以及 5种方法
中间件介绍: 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功 ...
- django框架--中间件系统
目录 零.参考 一.中间件的基本理解 二.中间件的系统定位 三.中间件的配置 四.中间件的执行流程 五.中间件与装饰器之间的思考 六.中间件的应用场景 七.内置中间件 八.总结 零.参考 https: ...
- 自学Python第二十二天- Django框架(三) AJAX、文件上传、POST 请求类型之间的转换、多APP开发、iframe、验证码、分页器、类视图、中间件、信号、日志、缓存、celery异步
Django官方文档 django 使用 AJAX django 项目中也可以使用 ajax 技术 前端 前端和其他 web 框架一样,需要注意的是,django 接收 POST 请求时,需要 csr ...
- Django框架学习索引
索引目录 1.Django介绍与安装 2.Django流程及模式 3.Django基本配置 4.Django模板(Templages) 1.Django的介绍和安装 3.Django框架模式 4.数据 ...
最新文章
- Script:挖掘AWR实现查询SCN历史增长走势
- 针对《评人工智能如何走向新阶段》一文,继续发布国内外的跟贴留言457-465条如下:
- mysql数据库出现无法登录(ERROR 1045 ),预防和解决及系列问题解决方法。
- PHP convet class to json data
- 服务器区分几位系统吗,怎么区分服务器是几个CPU,几核
- mysql error 1114,mysql error 1114 table is full 处理分享
- java 加载class文件路径_动手实现MVC: 1. Java 扫描并加载包路径下class文件
- [css] 怎样用纯CSS实现禁止鼠标点击事件?
- 推荐几款好用的模态框附带教程
- 微软彻底告别移动操作系统!
- 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)
- NLP学习03--递归神经网络RNN
- iOS:关于UIView切角的两种实现方式
- Shader 学习笔记:水面
- windows 屏幕保护色设置
- 华为云服务之弹性云服务器ECS的深度使用和云端实践
- 计算机d代表什么,DVI-D和DVI-I区别是什么?
- 竖版视频怎么批量转换成横版视频
- 青藤 #10115 栈练习1
- COTS应用程序开发框架简介(三)
热门文章
- Uncaught TypeError: XXX is not a function问题解决
- Unity_滚动文本字幕
- 郑州轻工业大学计算机学院书记,郑州轻工业大学计算机与通信工程学院导师教师师资介绍简介-陈浩然...
- 1.5 synchronized类锁和对象锁快速区别
- 学校校园学生寝室管理查寝打分系统 毕业设计毕设源码毕业论文开题报告参考(3)宿舍管理员功能
- PgSQL——学习笔记八: ORDER BY 子句:排序 GROUP BY 语句:分组
- 什么是压力测试,为什么要进行压力测试?Jmeter工具的使用
- Involution:新神经网络算子(CVPR2021 论文速读)
- Typora 建立内部跳转链接
- cocoscreator数字增长动画