django的FBV和CBV的装饰器例子
备忘
def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not u: return redirect('/login/') print("call index()") return func(request,*args,**kwargs) return inner #FBV @auth def index(request): #带签名的cookie obj = HttpResponse('') obj.set_signed_cookie('username','123123',salt='adasaa')#salt加盐,设置cookie,加密 request.get_signed_cookie('username','123123',salt='adasaa')#获取cookie u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request,'index.html',{'u':u}) #CBV from django.views import View from django.utils.decorators import method_decorator class Order(View): @method_decorator(auth) def get(self,request): u = request.COOKIES.get('username111') return render(request, 'index.html', {'u': u}) def post(self,request): u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request, 'index.html', {'u': u})
CBV如果有很多方法,都要加装饰器的话,可以加在dispatch方法上,这样类里面的方法就不用再一一加上装饰了
def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not u: print("未登陆") return redirect('/login/') print("call now") return func(request,*args,**kwargs) return inner #FBV @auth def index(request): #带签名的cookie obj = HttpResponse('') obj.set_signed_cookie('username','123123',salt='adasaa')#salt加盐,设置cookie,加密 request.get_signed_cookie('username','123123',salt='adasaa')#获取cookie u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request,'index.html',{'u':u}) #CBV from django.views import View from django.utils.decorators import method_decorator class Order(View): @method_decorator(auth) def dispatch(self, request, *args, **kwargs): return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request): u = request.COOKIES.get('username111') return render(request, 'index.html', {'u': u}) def post(self,request): u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request, 'index.html', {'u': u})
再来一个终极版的,不用重写dispatch,直接加在类上面:
def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not u: print("未登陆") return redirect('/login/') print("call now") return func(request,*args,**kwargs) return inner #FBV @auth def index(request): #带签名的cookie obj = HttpResponse('') obj.set_signed_cookie('username','123123',salt='adasaa')#salt加盐,设置cookie,加密 request.get_signed_cookie('username','123123',salt='adasaa')#获取cookie u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request,'index.html',{'u':u}) #CBV from django.views import View from django.utils.decorators import method_decorator @method_decorator(auth,name='dispatch') class Order(View): # @method_decorator(auth) # def dispatch(self, request, *args, **kwargs): # return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request): u = request.COOKIES.get('username111') return render(request, 'index.html', {'u': u}) def post(self,request): u = request.COOKIES.get('username111') if not u: return redirect('/login/') else: return render(request, 'index.html', {'u': u})