前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django之中间件

Django之中间件

作者头像
超蛋lhy
发布2018-08-31 16:09:58
3320
发布2018-08-31 16:09:58
举报
文章被收录于专栏:Pythonista

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

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

与mange.py在同一目录下的文件夹 wupeiqi/middleware下的auth.py文件中的Authentication类

中间件中可以定义四个方法,分别是:

  • 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)

以上方法的返回值可以是None和HttpResonse对象,如果是None,则继续按照django定义的规则向下执行,如果是HttpResonse对象,则直接将该对象返回给用户。

自定义中间件

1、settings.py

代码语言:javascript
复制
from django.middleware.common import CommonMiddleware

 2、views.py

代码语言:javascript
复制
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.
def test(request):
    print("test===")
    return HttpResponse("...666")

3、新建md.py

代码语言:javascript
复制
#!/usr/bin/env python 
# -*- coding:utf8 -*-
# from django.middleware.common import CommonMiddleware
from django.utils.deprecation import MiddlewareMixin

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_request")
        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

3.1 views.py 抓取异常

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding:utf8 -*-
# from django.middleware.common import CommonMiddleware
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import 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_request")
        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('错拉...')

3.1.1

代码语言:javascript
复制
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import 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')
        # 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')

    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')
    def process_response(self,request,response):
        print('m2.process_response')
        return response
    def process_exception(self, request, exception):
        print('m2.process_exception')
        return HttpResponse('错误了...')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义中间件
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档