详解Django中类视图使用装饰器的方式


Posted in Python onAugust 12, 2018

类视图使用装饰器

为类视图添加装饰器,可以使用两种方法。

为了理解方便,我们先来定义一个为函数视图准备的装饰器(在设计装饰器时基本都以函数视图作为考虑的被装饰对象),及一个要被装饰的类视图。

def my_decorator(func):
  def wrapper(request, *args, **kwargs):
    print('自定义装饰器被调用了')
    print('请求路径%s' % request.path)
    return func(request, *args, **kwargs)
  return wrapper

class DemoView(View):
  def get(self, request):
    print('get方法')
    return HttpResponse('ok')

  def post(self, request):
    print('post方法')
    return HttpResponse('ok')

4.1 在URL配置中装饰

urlpatterns = [
  url(r'^demo/$', my_decorate(DemoView.as_view()))
]

此种方式最简单,但因装饰行为被放置到了url配置中,单看视图的时候无法知道此视图还被添加了装饰器,不利于代码的完整性,不建议使用。

此种方式会为类视图中的所有请求方法都加上装饰器行为(因为是在视图入口处,分发请求方式前)。

4.2 在类视图中装饰

在类视图中使用为函数视图准备的装饰器时,不能直接添加装饰器,需要使用method_decorator将其转换为适用于类视图方法的装饰器。

method_decorator装饰器使用name参数指明被装饰的方法

# 为全部请求方法添加装饰器
@method_decorator(my_decorator, name='dispatch')
class DemoView(View):
  def get(self, request):
    print('get方法')
    return HttpResponse('ok')

  def post(self, request):
    print('post方法')
    return HttpResponse('ok')


# 为特定请求方法添加装饰器
@method_decorator(my_decorator, name='get')
class DemoView(View):
  def get(self, request):
    print('get方法')
    return HttpResponse('ok')

  def post(self, request):
    print('post方法')
    return HttpResponse('ok')

如果需要为类视图的多个方法添加装饰器,但又不是所有的方法(为所有方法添加装饰器参考上面例子),可以直接在需要添加装饰器的方法上使用method_decorator,如下所示

from django.utils.decorators import method_decorator

# 为特定请求方法添加装饰器
class DemoView(View):

  @method_decorator(my_decorator) # 为get方法添加了装饰器
  def get(self, request):
    print('get方法')
    return HttpResponse('ok')

  @method_decorator(my_decorator) # 为post方法添加了装饰器
  def post(self, request):
    print('post方法')
    return HttpResponse('ok')

  def put(self, request): # 没有为put方法添加装饰器
    print('put方法')
    return HttpResponse('ok')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
解决安装python库时windows error5 报错的问题
Oct 21 Python
详解django中url路由配置及渲染方式
Feb 25 Python
python机器人运动范围问题的解答
Apr 29 Python
Pytorch反向求导更新网络参数的方法
Aug 17 Python
Pytorch Tensor的索引与切片例子
Aug 18 Python
Django框架ORM数据库操作实例详解
Nov 07 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
Jan 18 Python
python动态文本进度条的实例代码
Jan 22 Python
PyCharm刷新项目(文件)目录的实现
Feb 14 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
May 11 Python
pycharm导入源码的具体步骤
Aug 04 Python
python 获取计算机的网卡信息
Feb 18 Python
python中pip的安装与使用教程
Aug 10 #Python
python3判断url链接是否为404的方法
Aug 10 #Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
Aug 10 #Python
Selenium元素的常用操作方法分析
Aug 10 #Python
Selenium定位元素操作示例
Aug 10 #Python
判断python字典中key是否存在的两种方法
Aug 10 #Python
详解python的sorted函数对字典按key排序和按value排序
Aug 10 #Python
You might like
php 代码优化之经典示例
2011/03/24 PHP
利用谷歌 Translate API制作自己的翻译脚本
2014/06/04 PHP
PHP遍历文件夹与文件类及处理类用法实例
2014/09/23 PHP
PHP中用Trait封装单例模式的实现
2019/12/18 PHP
准确获得页面、窗口高度及宽度的JS
2006/11/26 Javascript
javascript 写类方式之五
2009/07/05 Javascript
写出更好的JavaScript之undefined篇(上)
2009/11/22 Javascript
IE6图片加载的一个BUG解决方法
2010/07/13 Javascript
JS实现自动固定顶部的悬浮菜单栏效果
2015/09/16 Javascript
基于Jquery和html5实现炫酷的3D焦点图动画
2016/03/02 Javascript
JavaScript实现跟随滚动缓冲运动广告框
2017/07/15 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
JS函数内部属性之arguments和this实例解析
2018/10/07 Javascript
JS实现鼠标拖拽盒子移动及右键点击盒子消失效果示例
2019/01/29 Javascript
弱类型语言javascript开发中的一些坑实例小结【变量、函数、数组、对象、作用域等】
2019/08/07 Javascript
vue开发简单上传图片功能
2020/06/30 Javascript
[08:07]DOTA2每周TOP10 精彩击杀集锦vol.8
2014/06/25 DOTA
使用70行Python代码实现一个递归下降解析器的教程
2015/04/17 Python
python类继承用法实例分析
2015/05/27 Python
对python:threading.Thread类的使用方法详解
2019/01/31 Python
Python按钮的响应事件详解
2019/03/04 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
2019/08/29 Python
tensorboard显示空白的解决
2020/02/15 Python
python 线性回归分析模型检验标准--拟合优度详解
2020/02/24 Python
快速解决jupyter notebook启动需要密码的问题
2020/04/21 Python
PyCharm中如何直接使用Anaconda已安装的库
2020/05/28 Python
django跳转页面传参的实现
2020/09/17 Python
纯CSS3打造属于自己的“小黄人”
2016/03/14 HTML / CSS
以实惠的价格提供高品质的时尚:Newchic
2018/01/18 全球购物
King Apparel官网:英国街头服饰品牌
2019/09/05 全球购物
儿科护士自我鉴定
2013/10/14 职场文书
教师节倡议书2015
2015/04/27 职场文书
白银帝国观后感
2015/06/17 职场文书
掌握一个领域知识,高效学习必备方法
2019/08/08 职场文书
MySQL Router实现MySQL的读写分离的方法
2021/05/27 MySQL
Python可变与不可变数据和深拷贝与浅拷贝
2022/04/06 Python