# 装饰器定义 def auth(func): def inner(request,*args,**kwargs): v = request.COOKIES.get("user") if not v: return redirect("/login/") return func(request,*args,**kwargs) return inner # FBV 方式 # @auth # def index(request): # v = request.COOKIES.get("user") # return render(request,"index.html") # CBV 方式(一),直接在class内函数上装饰 from django import views from django.utils.decorators import method_decorator # class Order(views.View): # @method_decorator(auth) # def get(self,request): # v = request.COOKIES.get("user") # return render(request,"index.html") # # def post(self,request): # v = request.COOKIES.get("user") # return render(request,"index.html") # CBV 方式(二),直接在class起始dispatch函数上装饰 # class Order(views.View): # @method_decorator(auth) # def dispatch(self, request, *args, **kwargs): # return super(Order,self).dispatch(request,*args,**kwargs) # CBV 方式(三),直接在class上装饰 # @method_decorator(auth,name="dispatch") # class Order(views.View): # def dispatch(self, request, *args, **kwargs): # pass