django cbv添加装饰器

from django.views import View
from django.utils.decorators import method_decorator
"""
CBV中不建议你直接给类的方法加装饰器
无论该装饰器能否正常工作 都不建议直接加
"""
# @method_decorator(login_auth,name='get') # 给get请求添加装饰器
# @method_decorator(login_auth,name='post') # 方式二(可以添加多个,针对不同的方法加不同的装饰器)
class MyLogin(View):
@method_decorator(login_auth) # 方式三:它会直接作用于当前类里面所有的方法
def dispatch(self, request, *args, **kwargs):
pass

# @method_decorator(login_auth) # 方式一:指名道姓
def get(self,request):
return HttpResponse('get请求')

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

原文地址:https://www.cnblogs.com/ZhZhang12138/p/14875652.html