美文网首页
django_cookie装饰器

django_cookie装饰器

作者: 两点半的杂货铺 | 来源:发表于2018-01-28 17:30 被阅读27次
                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装饰器

          本文链接:https://www.haomeiwen.com/subject/frrzaxtx.html