django框架之中间件

Posted 白木潇潇夕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django框架之中间件相关的知识,希望对你有一定的参考价值。

中间件简介

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

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

如图:

中间件中一共有五个方法:

process_request

process_view

process_exception

process_response

process_template_response

1、中间件之process_request,process_response

process_request(self,request)

process_response(self, request, response)

当用户发起请求的时候会依次经过所有的的中间件,这个时候的请求时process_request,最后到达views的函数中,views函数处理后,在依次穿过中间件,这个时候是process_response,最后返回给请求者

在django中叫中间件,在其他web框架中,有的叫管道,httphandle

上述截图中的中间件都是django中的,我们也可以自己定义一个中间件,我们可以自己写一个类,但是必须继承MiddlewareMixin

所以需要导入:from django.utils.deprecation import MiddlewareMixin

我们在项目文件下创建一个Middle目录,并在下面创建md.py代码例子如下:

django项目的settings模块

md.py

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,render,HttpResponse
class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    def process_response(self,request,response):
        print("m1.process_response")
        return response
class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")

    def process_response(self, request, response):
        print("m2.process_response")
        return response
# 执行结果是:
#         M1.process_request
#         M2.process_request
#         m2.process_response
#         m1.process_response

views.py

from django.shortcuts import render,HttpResponse,redirect
from django.forms import Form
from django.forms import fields
from django.forms import widgets

def test(request):
    print("test")
    return HttpResponse("xuyuanyuan")

url.py

urlpatterns = [
    url(r\'^admin/\', admin.site.urls),
    url(r\'^test.html$\', views.test),

]

 

当页面发起请求时:

pycharm上面显示的运行结果是:

但是如果当请求到达请求2的时候直接不符合条件返回,程序将把请求直接发给中间件2返回,然后依次返回到请求者

用如下图进行理解:

当然这是在django1.10的时候,在之前的版本的时候是直接返回到最后一个中间件的response,然后向上依次返回,最后到发起请求

示例:

md.py

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,render,HttpResponse

class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M1.process_view")

    def process_response(self,request,response):
        print("m1.process_response")
        return response


class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")
        return M2.process_request
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M2.process_view")
        # response=callback(request,*callback_args,**callback_kwargs)
        # return response
    def process_response(self,request,response):
        print("m2.process_response")
        # return response


class M3(MiddlewareMixin):
    def process_request(self, request):
        print("M3.process_request")

    def process_view(self, request, callback, callback_args, callback_kwargs):
        print("M3.process_view")

    def process_response(self, request, response):
        print("m3.process_response")
        return response

执行结果是:

解释说明:

# 执行结果是:(  在django现在的版本中,因为在m2中 def process_request(self,request):
#         print("M2.process_request")
#         return M2.process_request有返回值,故而遇到返回值就直接找到自己的response返回)
# M1.process_request
# M2.process_request
# m2.process_response
# m1.process_response
# (  在django以前的版本,因为在m2中 def process_request(self,request):
#         print("M2.process_request")
#         return M2.process_request有返回值,故而遇到返回值就直接找到最后的一个response返回,
# 故而执行结果会变成:
# M1.process_request
# M2.process_request
# m3.process_response
# m2.process_response
# m1.process_response)

2、中间件之process_view

process_view(self, request, callback, callback_args, callback_kwargs)

我们在md.py文件中的的代码进行更改:

示例一:

md.py

class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    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.process_response")
        return response

class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M2.process_view")
        response=callback(request,*callback_args,**callback_kwargs)
        return response
    def process_response(self,request,response):
        print("m2.process_response")
        return response
# 执行结果是:
#         M1.process_request
#         M2.process_request
#         M1.process_view
#         M2.process_view
#         test
#         m2.process_response
#         m1.process_response

示例二:

class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    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.process_response")
        return response

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

# 执行结果是:
#         M1.process_request
#         M2.process_request
#         M1.process_view
#         test
#         m2.process_response
#         m1.process_response

关系如下图示例:

当最后一个中间的process_request到达路由关系映射之后,返回到中间件1的process_view,然后依次往下,到达views函数,最后通过process_response依次返回到达用户

3、中间件之process_exception

process_exception(self, request, exception)

当views的函数中出现错误时,就会执行process_exception方法

示例:

class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    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.process_response")
        return response
    def process_exception(self,request,exception):
        print("M1.process_exception")

class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M2.process_view")
        # response=callback(request,*callback_args,**callback_kwargs)
        # return response
    def process_response(self,request,response):
        print("m2.process_response")
        return response
    def process_exception(self,request,exception):
        print("M2.process_exception")
        return HttpResponse("异常处理")
# 执行结果是:
        # M1.process_request
        # M2.process_request
        # M1.process_view
        # M2.process_view
        # M2.process_exception
        # m2.process_response
        # m1.process_response

  

如果在中间中添加了process_exception方法,工作图示为:

这样当用户发起请求的时候到达中间件3的process_request之后会到达urls路由关系映射这里,如果匹配到了就会到中间件1的process_view,然后依次传递到中间件3的process_view,到达view函数。如果view函数中有报错,则会从中间件3依次向上判断每个中间件的process_exception是否能匹配到这个错误信息,如果匹配到则直接返回到最后一个中间件,这里即中间件3的process_response,然后依次返回到用户,如果没有匹配到这个错误则直接在页面显示错误信息。如果view函数中没有错误,则到中间3即最后一个中间件3的process_response,然后依次向上,传到用户

4、中间件之process_template_responseprocess

process_template_response(self,request,response)

只有当views函数中返回的对象中具有render方法,是就会直接process_template_responseprocess

示例一:

 views.py

from django.shortcuts import render,HttpResponse,redirect
from django.forms import Form
from django.forms import fields
# =======在函数中添加render,使md.py文件里的def process_template_response函数执行
class JSONResponse:
    def __init__(self,req,status,msg):
        self.req=req
        self.status=status
        self.msg=msg
    def render(self):
        import json
        ret={
            "status":self.status,
            "msg":self.msg
        }
        return HttpResponse(json.dump(ret))

def test(request):
    ret={}
    return JSONResponse(request,True,"哈哈哈哈,错了吧")

md.py

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,render,HttpResponse
class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M1.process_view")

    def process_response(self,request,response):
        print("m1.process_response")
        return response
    def process_exception(self,request,exception):
        print("M1.process_exception")

    def process_template_response(self,request,response):
        """
        如果视图函数的返回值中,有render方法,则会执行该函数
        :param request:
        :param response:
        :return:
        """
        print("M1.process_template_response")
        return response

class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M2.process_view")
        # response=callback(request,*callback_args,**callback_kwargs)
        # return response
    def process_response(self,request,response):
        print("m2.process_response")
        return response
    def process_exception(self,request,exception):
        print("M2.process_exception")
        return HttpResponse("异常处理")
    def process_template_response(self,request,response):
        """
        如果视图函数的返回值中,有render方法,则会执行该函数
        :param request:
        :param response:
        :return:
        """
        print("M2.process_template_response")
        return response
# 执行结果是:在页面显示“异常处理”,
#     def process_exception(self,request,exception):
#         print("M1.process_exception")该函数未执行,由于
#         def process_exception(self, request, exception):
#             print("M2.process_exception")
#             return HttpResponse("异常处理")
        # 在M2中有return返回值
# 终端上的执行结果是:
#         M1.process_request
#         M2.process_request
#         M1.process_view
#         M2.process_view
#         M2.process_template_response
#         M1.process_template_response
#         M2.process_exception
#         m2.process_response
#         m1.process_response

页面上的结果为:

后台显示:

示例二:

views.py

from django.shortcuts import render,HttpResponse,redirect
from django.forms import Form
from django.forms import fields
from django.forms import widgets


# =======在函数中添加render,使md.py文件里的def process_template_response函数执行
class JSONResponse:
    def __init__(self,req,status,msg):
        self.req=req
        self.status=status
        self.msg=msg
    def render(self):
        import json
        ret={
            "status":self.status,
            "msg":self.msg
        }
        return HttpResponse(json.dump(ret))

def test(request):
    ret={}
    return JSONResponse(request,True,"哈哈哈哈,错了吧")

 md.py

class M1(MiddlewareMixin):
    def process_request(self,request):
        print("M1.process_request")

    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M1.process_view")

    def process_response(self,request,response):
        print("m1.process_response")
        return response
    def process_exception(self,request,exception):
        print("M1.process_exception")
        return HttpResponse("异常处理")
    def process_template_response(self,request,response):
        """
        如果视图函数的返回值中,有render方法,则会执行该函数
        :param request:
        :param response:
        :return:
        """
        print("M1.process_template_response")
        return response

class M2(MiddlewareMixin):
    def process_request(self,request):
        print("M2.process_request")
    def process_view(self,request,callback,callback_args,callback_kwargs):
        print("M2.process_view")
        # response=callback(request,*callback_args,**callback_kwargs)
        # return response
    def process_response(self,request,response):
        print("m2.process_response")
        # return response
    def process_exception(self,request,exception):
        print("M2.process_exception")

    def process_template_response(self,request,response):
        """
        如果视图函数的返回值中,有render方法,则会执行该函数
        :param request:
        :param response:
        :return:
        """
        print("M2.process_template_response")
        return response
# 执行结果是:在页面显示“异常处理”,所有的函数都执行了
# 终端上的执行结果是:
#         M1.process_request
#         M2.process_request
#         M1.process_view
#         M2.process_view
#         M2.process_template_response
#         M1.process_template_response
#         M2.process_exception
#         M1.process_exception
#         m2.process_response
#         m1.process_response

 

 

以上是关于django框架之中间件的主要内容,如果未能解决你的问题,请参考以下文章

django框架之中间件

Django框架之中间件MiddleWare

Django框架 之 中间件

Django框架之中间件CSRF跨站请求伪造

Django框架

小猿圈python之Django和Flask比较?