django ----CBV中加装饰器

时间:2022-05-22 20:07:32

CBV中加装饰器

from django import views
from django.utils.decorators import method_decorator
def login_auth(func):
def inner(request,*args,**kwargs):
next_url=request.get_full_path()
if request.COOKIES.get('is_login'):
return func(request,*args,**kwargs)
else:
return redirect('cookie_login/?next=%s'%next_url)
return inner
# @method_decorator(login_auth,name='get')
# @method_decorator(login_auth,name='post')
class UserList(views.View):
# @method_decorator(login_auth)
def dispatch(self, request, *args, **kwargs):
obj=super().dispatch(request, *args, **kwargs)
return obj @method_decorator(login_auth)
def get(self,request):
return HttpResponse('我是用户列表') def post(self,request):
return HttpResponse('我是用户列表')