django_cookie装饰器

            FBV:
                def auth(func):
                    def inner(reqeust,*args,**kwargs):
                        v = reqeust.COOKIES.get('username111')
                        if not v:
                            return redirect('/login/')
                        return func(reqeust, *args,**kwargs)
                    return inner
        
            CBV:
                from django import views
                from django.utils.decorators import method_decorator

                @method_decorator(auth,name='dispatch')
                class Order(views.View):

                    # @method_decorator(auth)
                    # def dispatch(self, request, *args, **kwargs):
                    #     return super(Order,self).dispatch(request, *args, **kwargs)

                    # @method_decorator(auth)
                    def get(self,reqeust):
                        v = reqeust.COOKIES.get('username111')
                        return render(reqeust,'index.html',{'current_user': v})

                    def post(self,reqeust):
                        v = reqeust.COOKIES.get('username111')
                        return render(reqeust,'index.html',{'current_user': v})

你可能感兴趣的:(django_cookie装饰器)