1. 程式人生 > >django ----CBV中加裝飾器

django ----CBV中加裝飾器

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('我是使用者列表')