Django笔记(中间件)

中间件:是一个轻量级的底层插件,可以介入Django的请求和响应过程(面向切片编程)

本质就是一个python类

设置中间件:在工程的目录下新建一个middlewares的文件夹,再新建一个.py文件

在里面继承  MiddlewareMixin 的类

from django.utils.deprecation import MiddlewareMixin

再定义自己的类名

最后在settings.py文件里添加MIDDLEWARE配置项

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.filterusermiddleware.MyMiddleWare',
    'middlewares.filterusermiddleware.MyWareA',
    'middlewares.filterusermiddleware.MyWareB',
    'middlewares.filterusermiddleware.MyWareC',

]

中间价之process_requset(self, request)、process_response(self, request, response)

示例代码

from django.shortcuts import render
from django.utils.deprecation import MiddlewareMixin


class MyWareA(MiddlewareMixin):
    def process_request(self, req):
        print("中间件1的请求")
    def process_response(self, req, response):
        print("中间件1的返回")
        return response

class MyWareB(MiddlewareMixin):
    def process_request(self, req):
        print("中间件2的请求")
    def process_response(self, req, response):
        print("中间件2的返回")
        return response

class MyWareC(MiddlewareMixin):
    def process_request(self, req):
        print("中间件3的请求")
    def process_response(self, req, response):
        print("中间件3的返回")
        return response

运行结果如下

Django笔记(中间件)_第1张图片

表示发送请求是先把process_request先打印出来,再从最后一个process_request的同级类里打印process_response,以此类推。

ip白名单示例

from django.shortcuts import render
from django.utils.deprecation import MiddlewareMixin


class MyMiddleWare(MiddlewareMixin):
    def process_request(self, req):
        ip = req.META.get("REMOTE_ADDR")
        # 白名单
        white_ips = [
            '61.144.96.111'
        ]
        if ip in white_ips:
            return render(req, 'home.html', {'data': []})

views视图逻辑

def home(req):
    res = ['美女荷官']
    return render(req, 'home.html', {'data': res})

前端逻辑

{% for i in data %}
    

{{ i }}


{% empty %}

{% endfor %}

中间件之process_view

示例代码

class MyWareA(MiddlewareMixin):
    def process_request(self, req):
        print("中间件1的请求")
    def process_response(self, req, response):
        print("中间件1的返回")
        return response

    def process_view(self, req, callback, callback_args, callback_kwargs):
        print("中间件1的view")


class MyWareB(MiddlewareMixin):
    def process_request(self, req):
        print("中间件2的请求")
    def process_response(self, req, response):
        print("中间件2的返回")
        return response
    def process_view(self, req, callback, callback_args, callback_kwargs):
        print("中间件2的view")

class MyWareC(MiddlewareMixin):
    def process_request(self, req):
        print("中间件3的请求")
    def process_response(self, req, response):
        print("中间件3的返回")
        return response
    def process_view(self, req, callback, callback_args, callback_kwargs):
        print("中间件3的view")

运行结果

Django笔记(中间件)_第2张图片

表示在process_request运行完了之后,通过urls,返回到第一个中间件的process_view,然后运行完process_view之后,再在最后一个中间件先返回process_response.

中间件之process_template_response(self,req,response)

其中request 是 HttpRequest 对象, response 是一个由Django view或者中间件返回的TemplateResponse 对象,process_template_response()在 view 使用 render 渲染一个模版对象完成之后被调用,它必须返回一个render 方法执行后的response对象。

你可能感兴趣的:(Django)