详解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中将阿拉伯数字转换成中文的实现代码
May 19 Python
python实现读取excel写入mysql的小工具详解
Nov 20 Python
用TensorFlow实现多类支持向量机的示例代码
Apr 28 Python
pandas修改DataFrame列名的实现方法
Feb 22 Python
使用python实现简单五子棋游戏
Jun 18 Python
浅析Django中关于session的使用
Dec 30 Python
python如何实现不可变字典inmutabledict
Jan 08 Python
Python计算公交发车时间的完整代码
Feb 12 Python
python输出pdf文档的实例
Feb 13 Python
Python json读写方式和字典相互转化
Apr 18 Python
Python常用数据分析模块原理解析
Jul 20 Python
浅谈python数据类型及其操作
May 25 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
ob_start(),ob_start('ob_gzhandler')使用
2006/12/25 PHP
jQuery中的RadioButton,input,CheckBox取值赋值实现代码
2014/02/18 PHP
windwos下使用php连接oracle数据库的过程分享
2014/05/26 PHP
PHP实现的构造sql语句类实例
2016/02/03 PHP
$.ajax json数据传递方法
2008/11/19 Javascript
Jquery自定义button按钮的几种方法
2014/06/11 Javascript
jquery隔行换色效果实现方法
2015/01/15 Javascript
node.js [superAgent] 请求使用示例
2015/03/13 Javascript
JavaScript阻止回车提交表单的方法
2015/12/30 Javascript
AngularJS入门教程之链接与图片模板详解
2016/08/19 Javascript
js基于FileSaver.js 浏览器导出Excel文件的示例
2017/08/15 Javascript
js构建二叉树进行数值数组的去重与优化详解
2018/03/26 Javascript
原理深度解析Vue的响应式更新比React快
2020/04/04 Javascript
[00:32]2018DOTA2亚洲邀请赛Mineski出场
2018/04/04 DOTA
11个并不被常用但对开发非常有帮助的Python库
2015/03/31 Python
对于Python的Django框架部署的一些建议
2015/04/09 Python
python实现判断一个字符串是否是合法IP地址的示例
2018/06/04 Python
Python判断一个list中是否包含另一个list全部元素的方法分析
2018/12/24 Python
Python 格式化打印json数据方法(展开状态)
2020/02/27 Python
Python网络爬虫信息提取mooc代码实例
2020/03/06 Python
文件上传服务器-jupyter 中python解压及压缩方式
2020/04/22 Python
python将logging模块封装成单独模块并实现动态切换Level方式
2020/05/12 Python
python爬虫用scrapy获取影片的实例分析
2020/11/23 Python
英国香水店:The Perfume Shop
2017/03/27 全球购物
手机配件第一品牌:ZAGG
2017/05/28 全球购物
德国传统玻璃制造商:Cristalica
2018/04/23 全球购物
NYX Professional Makeup英国官网:美国平价专业彩妆品牌
2019/11/13 全球购物
JBL美国官方商店:扬声器、耳机等
2019/12/01 全球购物
声明struct x1 { . . . }; 和typedef struct { . . . }x2;有什么不同
2012/06/02 面试题
SOA的常见陷阱或者误解是什么
2014/10/05 面试题
音乐表演专业毕业生求职信
2013/10/14 职场文书
就业表自我评价分享
2014/02/06 职场文书
行政人事专员岗位职责
2014/03/05 职场文书
装饰施工员岗位职责
2015/04/11 职场文书
部门2015年度工作总结
2015/04/29 职场文书
焦裕禄纪念馆观后感
2015/06/09 职场文书