CBV添加装饰器

CBV添加装饰器

from django.utils.decorators import method_decorator

(1)添加在函数上

class CbvTest(View):
    @method_decorator(login_auth)
    def get(self):
        return HttpResponse('get请求')

    def post(self):
        return HttpResponse('post请求')

(2)添加在类上

@method_decorator(login_auth,name='get')
@method_decorator(login_auth,name='post')
class CbvTest(View):
    def get(self):
        return HttpResponse('get请求')

    def post(self):
        return HttpResponse('post请求')

(3)重写dispatch方法

  • 作用于当前类里面的所有的方法
class CbvTest(View):
    @method_decorator(login_auth)
    def dispatch(self, request, *args, **kwargs):
        pass

    def get(self):
        return HttpResponse('get请求')

    def post(self):
        return HttpResponse('post请求')
posted @ 2024-03-29 09:55  ssrheart  阅读(1)  评论(0编辑  收藏  举报