当前位置:网站首页>第八单元 中间件
第八单元 中间件
2022-08-02 14:01:00 【czy1206527605】
一.中间件
是什么
Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,全局修改Django的输入或输出。
干什么
如果你想修改请求,例如被传送到view中的HttpRequest对象。 或者你想修改view返回的HttpResponse对象,这些都可以通过中间件来实现。
在哪里:
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',
]
二.中间件的原理与应用
中间件的原理
常用的钩子方法
process_request(self,request) # django 处理请求之前,会执行该方法
process_response(self, request, response) # django返回响应之后,执行该方法
定义与调用
创建定义中间件位置
自定义的中间件
第一个def
处理请求之前调用这个中间件
第二个def
视图返回相应之后返回这个中间件
三.中间件的注册与使用效果
中间件的注册
中间件的效果
正常的效果
错误的效果
边栏推荐
- Sentinel源码(六)ParamFlowSlot热点参数限流
- 世界上最大的开源基金会 Apache 是如何运作的?
- 方舟生存进化淘宝面板服务器是怎么一回事?
- drf序列化器-Serializer
- FFmpeg 的AVCodecContext结构体详解
- Flask-SQLAlchemy
- About the development forecast of the market outlook?2021-05-23
- You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
- Shell脚本完成pxe装机配置
- 第二讲 软件生命周期
猜你喜欢
Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush
CVE-2020-27986(Sonarqube敏感信息泄漏) 漏洞修复
stack && queue
网络安全第四次作业
Diodes and their applications
如何解决mysql服务无法启动1069
The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
Shell脚本完成pxe装机配置
Flask项目的完整创建 七牛云与容联云
redis分布式锁和看门狗的实现
随机推荐
xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
Diodes and their applications
Mysql's case the when you how to use
rust使用mysql插入数据
二极管及其应用
MySQL数据库语法格式
The future of financial services will never stop, and the bull market will continue 2021-05-28
【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
ping命令的使用及代码_通过命令查看ping路径
微信小程序-最近动态滚动实现
Tornado框架路由系统介绍及(IOloop.current().start())启动源码分析
定了!就在7月30日!
IDEA打包jar包
苏州大学:从 PostgreSQL 到 TDengine
els strip collision deformation judgment
Flask框架深入一
What are the file encryption software?Keep your files safe
你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
瑞吉外卖笔记——第05讲Redis入门
Supervision strikes again, what about the market outlook?2021-05-22