欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

django中视图函数中装饰器

程序员文章站 2023-11-14 17:34:40
方法一 给指定方法加 方法二 给dispatch加 方法三 给类加 ......

方法一

给指定方法加

from django.utils.decorators import method_decorator

class xx(view):
    @method_decorator(装饰器方法)
    def post(self, request):
                    ...

方法二

给dispatch加

@method_decorator(装饰器方法)
def dispatch(self, request, *args, **kwargs):
    ...

方法三

给类加

from django.utils.decorators import method_decorator
@method_decorator(装饰器方法, name="get")
@method_decorator(装饰器方法, name="post")
class xxxx(view):
    ...