zoukankan      html  css  js  c++  java
  • FBV和CBV装饰器

    FBV装饰器:

    def cook(request):
    
        err_msg=""
        if request.method == "GET":
            return render(request,'cookie.html')
    
        if request.method == "POST":
    
            username = request.POST.get('username')
            p = request.POST.get('password')
    
            dic = user_info.get(username)
            print(dic)
    
            if not dic:
                err_msg="用户不存在"
                return render(request,'cookie.html',{'err_msg':err_msg})
    
            if dic['pwd'] == int(p):
    
                res = redirect('/xiaoqing/cookie1')
                # res.set_cookie('username_cookie',username)   #设置cookie   关闭浏览器失效
                # res.set_cookie('username_cookie',username,max_age=10)   设置cookie失效时间 10秒过期
                import datetime
                current_date=datetime.datetime.utcnow()
                change_date=current_date+datetime.timedelta(seconds=5)
                res.set_cookie('username_cookie',username,expires=change_date)   #到哪个时间失效
    
                # res.set_signed_cookie('username_cookie',username,salt='sdasdas')
    
                return res
            else:
                return render(request,'cookie.html')
    cook函数set cookie
    def auth(func):  #装饰器 cookie认证
    
        def inner(request,*args,**kwargs):
            v = request.COOKIES.get('username_cookie')
            print(v)
            if not v:
                return redirect('/xiaoqing/cookie')
    
            return func(request,*args,**kwargs)
    
    
        return inner
    
    @auth
    def cook1(request):
        #获取当前已经登录的用户
    
        v=request.COOKIES.get('username_cookie')   #获取cookie
        # 或者 v=request.COOKIES['username_cookie']  #获取cookie
    
        # v=request.get_signed_cookie('username_cookie',salt='sdasdas')   #获取加密cookie
    
    
        return render(request,'cookie1.html',{'current_user':v,})

    CBV装饰器

    def auth(func): #装饰器
    
        def inner(request,*args,**kwargs):
            v = request.COOKIES.get('username_cookie')
            print(v)
            if not v:
                return redirect('/xiaoqing/cookie')
    
            return func(request,*args,**kwargs)
    
    
        return inner
    
    from django import views
    
    from django.utils.decorators import method_decorator  #导入方法
    
    @method_decorator(auth,name='dispatch')  #类中全部方法做认证
    class Order(views.View):
    
    
        # @method_decorator(auth)  #单独方法做认证
        def get(self,request):
            v=request.COOKIES.get('username_cookie')
    
    
            return render(request,'cookie1.html',{'current_user':v,})
    
    
        def post(self,request):
            pass
  • 相关阅读:
    植物大战僵尸英文原版
    2020-5-13递归练习 六人参加竞赛
    2020-5-1递归练习
    java当脚本用。QQ表白轰炸机
    面向实战---VUE项目的文件加载顺序,或者加载流程,以及index.html,main.js,app.vue的区别
    CSS多个view随机分布,不重叠,如何实现呢?
    vite项目才踩坑日志1
    css属性之clip-path
    纯CSS3实现的阳光海鸥沙滩遮阳伞和比基尼美女风景动画效果源码
    TP5 中使用wherein 进行查询,太慢了,怎么优化?
  • 原文地址:https://www.cnblogs.com/sunhao96/p/8980476.html
Copyright © 2011-2022 走看看