第十五章 中间件

  • 中间件:
  • 是一个用来处理django请求和响应的框架级别的钩子,它是一个轻量、低级别的插件系统,用于在全局范围内改变Django的输入和输出。

  • 每个中间件组件都负责做一些特定的功能。

  • 本质上是一个类。

  • request对象是wsgi封装进行传递给django

五个方法

  • 五个方法,四个特征(执行时间顺序参数返回值)

  • 中间件的方法可以选择性使用,中间件的类需要继承MiddlewareMixin

  • 中间件可以放于任意位置,需要在settings中进行注册,具体到中间件类名

# 注册中间件, settings.py
MIDDLEWARE=[
  'app01.mymiddleware.middleware.Md1',
  'app01.mymiddleware.middleware.Md2',
]
  • MIDDLEWARE配置项是一个列表,列表中是一个个字符串,这些字符串其实是一个个类,也就是一个个中间件
# app01.mymiddleware中的middleware.py
# 导入类
from django.shortscut import HttpResponse
from django.utils.deprecation import MiddlewareMixin

1. process_request()

  • 参数:self, request

1. 示例
# md1中间件
class Md1(MiddlewareMixin):
    def process_request(self, request):
        print('This is Md1 process_request')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md1")

# md2中间件
class Md2(MiddlewareMixin):
    def process_request(self, request):
        print('This is Md2 process_request, Md2')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md2")
2. 特征
  1. 执行时间:视图函数之前

  2. 参数:request(最终传递给view函数) 和视图函数是同一个request对象

  3. 执行顺序:按照注册的顺序执行

  4. 返回值:

    • None的情况是正常流程

    • 返回HttpResponse,执行当前process_request方法,倒序执行其他中间件的process_response方法,响应给浏览器

2. process_view()

  • 参数:self, request,view_func, view_args, view_kwargs

1. 示例
# md1中间件
class Md1(MiddlewareMixin):
      def process_view(self, *args, **kwargs):
        print('This is Md1 process_view')

# md2中间件
class Md2(MiddlewareMixin):
    def process_view(self, *args, **kwargs):
        print('This is Md2 process_view, Md2')
2. 特征
  1. 执行时间:视图函数之前process_request之后

  2. 参数:

    • request和视图函数是同一个request对象

    • view_func:视图函数(路由匹配得到的视图函数)

    • view_args:视图的位置参数(路由匹配时的分组)

    • view_kwargs:视图的关键字参数

  3. 执行顺序:按照注册的顺序执行

  4. 返回值:

    • None的情况是正常流程

    • HttpResponse,执行当前process_view方法,倒序执行最后中间件process_view方法,响应给浏览器

3. process_response()

  • 参数:self, request, response

  • 必须有返回值,response

1. 示例
# Md1中间件
class Md1(MiddlewareMixin):

    def process_request(self, request):
        print('This is Md1 process_request')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md1")

    def process_view(self, *args, **kwargs):
        print('This is Md1 process_view')
        
    def process_response(self, request, response):
        print('This is Md1 process_response')
        return response

# Md2中间件
class Md2(MiddlewareMixin):

    def process_request(self, request):
        print('This is Md2 process_request, Md2')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md2")

    def process_view(self, *args, **kwargs):
        print('This is Md2 process_view, Md2')

    def process_response(self, request, response):
        print('This is Md2 process_response, Md2')
        return response
2. 特征
  1. 执行时间:视图函数之后

  2. 参数:

    • request和视图函数是同一个request对象

    • response返回给浏览器的的响应对象

  3. 执行顺序:按照注册的倒序执行

  4. 返回值:

    • response处理完成后必须返回response对象

4. prcess_exeception()

  • 参数:self, request, exception

1. 示例
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin

class Md1(MiddlewareMixin):

    def process_request(self, request):
        print('This is Md1 process_request')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md1")

    def process_view(self, *args, **kwargs):
        print('This is Md1 process_view')

    def process_response(self, request, response):
        print('This is Md1 process_response')
        return response

    def process_exception(self, request, exception):
        print('This is Md1 process_response')
        print(exception)
        return HttpResponse('错误')


class Md2(MiddlewareMixin):

    def process_request(self, request):
        print('This is Md2 process_request, Md2')
        # print(id(request))
        # return HttpResponse("here's process_request method of Md2")

    def process_view(self, *args, **kwargs):
        print('This is Md2 process_view, Md2')

    def process_response(self, request, response):
        print('This is Md2 process_response, Md2')
        return response

    def process_exception(self, request, exception):
        print('This is Md2 process_response, Md2')
        print(exception)
        return HttpResponse('错误,Md2')
2. 特征
  1. 触发条件:视图层面有错误,视图函数之后

  2. 参数:

    • request和视图函数是同一个request对象

    • exception:捕获的异常对象

  3. 执行顺序:按照注册的倒序执行

  4. 返回值:

    • None时,交给下一个中间件继续处理,如果都有,则交由django处理

    • 如果有返回值,则继续倒序执行process_response方法

5. process_template_response()

  • 参数 :self, request, response

1. 示例
# mymiddleware.py
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin


class Md1(MiddlewareMixin):
    def process_template_response(self, request, response):
        print("here's process_template_response of Md1")
        # 渲染的字典数据
        response.context_data['name'] = 'henry'
        return response

class Md2(MiddlewareMixin):
    def process_template_response(self, request, response):
        print("here's process_template_response of Md2")
        # 需要渲染的模版名
        response.template_name='test.html'
        return response
# viwes.py
from django.shortcuts import render
from django.template.response import TemplateResponse

def index(request):
    print('here is index')
    return TemplateResponse(request, 'index.html', {'name': 'herny', 'age': 19})
2. 特征
  1. 触发条件:视图返回一个templateResponse对象,视图函数之后

  2. 参数:

    • request和视图函数是同一个request对象

    • response:templateResponse对象

  3. 执行顺序:按照注册的倒序执行

  4. 返回值

    • 必须返回HttpResponse对象,即return reponse

posted @ 2020-05-28 21:12  炜琴清  阅读(120)  评论(0编辑  收藏  举报