类视图使用装饰器

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

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

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

Django中类视图使用装饰器的方式的更多相关文章

  1. django类视图的装饰器验证

    django类视图的装饰器验证 django类视图的get和post方法是由View内部调用dispatch方法来分发,最后调用as_view来完成一个视图的流程. 函数视图可以直接使用对应的装饰器 ...

  2. Django:(05)类视图,装饰器和中间件

    一.类视图的定义和使用 在Django中还可以通过类来定义一个视图,称为类视图. 定义一个类视图:定义一个类,需继承 Django 提供的 View 类 . from django.views.gen ...

  3. Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request对象方法,属性和Response对象,form表单的上传

    Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request请求对象方法,属性和Response响应对象,form表单的上传 一丶CBV和FBV       在Django中存 ...

  4. django 内置用户-装饰器

    """ 一.如何给python内置用户添加额外的字段,注意一定义在没有迁移数据之前定义,否则会报错 1.在models中先调用 from django.contrib.a ...

  5. pyqt5界面与逻辑分离--信号槽的装饰器实现方式

    本文展示了 pyqt5 信号槽的装饰器实现方式(借鉴自 eirc6) 一个简单的例子.实现功能:两个数相加,显示结果.如图 两个文件,第一个是界面文件 ui_calc.py # ui_calc.py ...

  6. Django基础之给视图加装饰器

    1. 使用装饰器装饰FBV FBV本身就是一个函数,所以和给普通的函数加装饰器无差: def wrapper(func): def inner(*args, **kwargs): start_time ...

  7. Django学习手册 - 登录装饰器

    # 装饰器定义 def auth(func): def inner(request,*args,**kwargs): v = request.COOKIES.get("user") ...

  8. Django-给视图加装饰器

    给FBV加装饰器 FBV:function based view FBV本身就是一个函数,所以跟普通函数加装饰器是一样的 # 装饰函数是要在APP文件中定义,本例是在app01\templatetag ...

  9. django ----CBV中加装饰器

    CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...

随机推荐

  1. 伪类 :after 清除浮动的原理和方法

    浮动元素容器的clearing问题1. 问题的由来有这样一种情形:在一个容器(container)中,有两个浮动的子元素.<div>        <div style=" ...

  2. QT解决视频透视,有阴影的方法

    #define BG_DEVNAME "/dev/fb0"#define FG_DEVNAME "/dev/fb1" 课题5, QT界面与视频透明叠加问题:颜色 ...

  3. yum安装memchache

    转载地址:http://www.cnblogs.com/jiunadianshi/articles/2001334.html 标准的CentOS5软件仓库里面是没有memcache相应的包的,所以,我 ...

  4. python3.6连接mysql或者mariadb

    python3.6版本的安装查看上一篇文章 mysql或mariadb数据库的安装查看以前的文章,这里不再赘述 首先在mariadb数据库中创建相应的库和表: MariaDB [(none)]> ...

  5. MiniTools在ubuntu下快捷方式

    解压MiniTools-Linux-20140317.tgz root@ubuntu:~/tiny4412/MiniTools-20140317# ls -l total 38008 -rw-r--r ...

  6. Hive表的修改Alter

    1.查看创建表的信息 [show create table] hive> show create table student; OK createtab_stmt CREATE TABLE `s ...

  7. git-修改远程的URL

    git remote set-url命令修改remote URL git remote set-url传递两个参数 remote name.例如,origin或者upstream new remote ...

  8. SaltStack安装Redis-第十篇

    实验环境 node1  192.168.56.11   角色  salt-master node2  192.168.56.12   角色  salt-minon 完成内容 Salt远程安装Redis ...

  9. python sklearn.cross_validation 模块导入失败

    参考链接: https://blog.csdn.net/Jae_Peng/article/details/79277920 解决办法: 原来在 cross_validation 里面的函数都放在 mo ...

  10. Redis之Redis

    Redis 环境安装 安装 如果已经安装了老版本3.0.6 1. 卸载软件 sudo apt-get remove redis-server 2. 清除配置 sudo apt-get remove - ...