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请求')
标签:return,get,self,添加,CBV,HttpResponse,post,装饰,def
From: https://www.cnblogs.com/ssrheart/p/18103141