首页 > 其他分享 >在django如何给CBV添加装饰器?

在django如何给CBV添加装饰器?

时间:2022-12-06 16:33:35浏览次数:54  
标签:return Mydecorator 装饰 添加 CBV def django method decorator

在Django中,给CBV添加装饰器有几种方式?

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

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

一、添加装饰器的方法

# urls.py
urlpatterns = [
 
    path("login/",views.Mydecorator(views.MyView.as_view())),
]


# views.py

def Mydecorator(func):
    def wrapper(*args,**kwargs):
        print('你被装饰器装饰啦!')
        res = func(*args,**kwargs)
        return res
    return wrapper

# @method_decorator(Mydecorator,name="post")
# @method_decorator(Mydecorator,name="get")
class MyView(View):
    # @method_decorator(Mydecorator)
    def get(self,request):
        return HttpResponse("get请求")

    def post(self,request):
        return HttpResponse("post请求")

   在url中进行添加装饰器。

 

二、添加装饰器的方法

def Mydecorator(func):
    def wrapper(*args,**kwargs):
        print('你被装饰器装饰啦!')
        res = func(*args,**kwargs)
        return res
    return wrapper

@method_decorator(Mydecorator,name="post")
@method_decorator(Mydecorator,name="get")
class MyView(View):
    # @method_decorator(Mydecorator)
    def get(self,request):
        return HttpResponse("get请求")

    def post(self,request):
        return HttpResponse("post请求")

  通过装饰视图类,name="函数名称"。

三、添加装饰器的方法

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

def Mydecorator(func):
    def wrapper(*args,**kwargs):
        print('你被装饰器装饰啦!')
        res = func(*args,**kwargs)
        return res
    return wrapper

# @method_decorator(Mydecorator,name="post")
# @method_decorator(Mydecorator,name="get")
class MyView(View):
    @method_decorator(Mydecorator)
    def get(self,request):
        return HttpResponse("get请求")

    def post(self,request):
        return HttpResponse("post请求")

  

 

标签:return,Mydecorator,装饰,添加,CBV,def,django,method,decorator
From: https://www.cnblogs.com/shaoyishi/p/16955710.html

相关文章

  • vue 监听滚动事件到某个位置删除或者添加元素
    首先添加监听事件window.addEventListener('scroll',this.handleScroll)事件处理handleScroll(){varscrollTop=window.pageYOffset||document.......
  • Thrift RPC添加access log
    前言:当我们在部署web服务的时候,web容器通常都会记录来自客户端的访问日志。而当我们使用ThriftRPC服务的时候,Thrift服务则不会给我们自动记录客户端的访问日志。通过这......
  • Android高仿网易新闻客户端之动态添加标签
    承接上一篇文章:​​Android高仿网易新闻客户端之首页​​,今天来实现动态添加标签效果。动态标签页是一个流式布局,实现了宽度自动换行高度自动分配的功能,代码如下:FlowLayout.......
  • 解决添加authorization 请求头后跨域问题
    在做身份认证的时候前端请求头上增加authorization属性后报以下错误: AccesstoXMLHttpRequestat'http://127.0.0.1:500/api/login'fromorigin'http://127.0.0.1:......
  • vue-quill-editor富文本编辑器使用(带图片上传至七牛云,后端为django)
    vue-quill-editor富文本编辑器使用(带图片上传至七牛云,后端为django)1.安装vue-quill-editornpminstallvue-quill-editor-S2.引入到项目中有两种挂载方式:全局挂载......
  • QT添加下拉框
    1.在ui界面上有一个名为comboBox_content的下拉框,向其添加四个下拉内容,QModbusDataUnit是QModbusTcpClient的成员//四种通信内容:线圈(Coil)、离散量输入(DiscreteInputs)、输入......
  • IDEA中添加普通web项目
    【介绍】   之前一直在用eclipse后来换了idea效率提高了很多,当然项目也由原来的普通web项目更新成了maven项目效率又提高了不少,然而公司的一......
  • 添加es的mapping与setting
    一:建立索引请求方式:put 请求url:127.0.0.1:9200/rain_run_index 请求体:{"mappings":{"properties":{"deviceNo":{......
  • mysql8 添加用户,赋予表操作权限
    1.mysql8修改了安全规则,不能像mysql5.7一次性创建用户并授权,需要分批创建1.创建用户createuser'username'@'host'identifiedby'password'创建用户说明:1.use......
  • python django shell 更新代码后需要重启
    pythondjango 使用pythonmanage.py shell练习时更改代码后,需要重新打开窗口进行练习TRANSLATEwithxEnglishArabicHebrewPolishBulgarianH......