zoukankan      html  css  js  c++  java
  • django笔记10 cookie整理

    感谢武沛齐老师 Alex老师
    cookie  没有cookie所有的网站都登录不上
        客户端浏览器上的一个文件
            {'user':'ljc'}
            {"user":'zpt'}
            request.COOKIES.get('..')
            response.set_cookie('..')
    
            加密
            obj = HttpResponse('s')
            obj.set_signed_cookie('username',"kangbazi",salt="afajkfa")
    
            解密
            var w = request.get_signed_cookie('username',salt="afajkfa")
    
            import hashlib
            m = hashlib.md5('fdaf')
            m.update('afda')
    
    装饰器:
    
            def auth(func):    #装饰器
                def inner(request,*args, **kwargs):
                    v = request.COOKIES.get('username111')
                    if not v:
                        return redirect('/login/')
                    return func(request,*args, **kwargs)
                return inner
    
            FBV:
            def auth(func):
                def inner(request,*args, **kwargs):
                    v = request.COOKIES.get('username111')
                    if not v:
                        return redirect('/login/')
                    return func(request,*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,request):
                    v = request.COOKIES.get('username111')
                    return render(request, 'index.html', {'current_user': v})
    
                def post(self,request):
                    v = request.COOKIES.get('username111')
                    return render(request, 'index.html', {'current_user': v})
    cookie1:
        request.COOKIES.get('username111')  #获取用户端发来的cookie  获取
    
        response = render(request,"index.html")
        response = redirect('/index/')
        #设置cookie 关闭浏览器失效(还可以设置超时时间)
        response.set_cookie('key',"value")  #关闭浏览器时 才失效     设置
        return response
    
    cookie2:
        request.COOKIES.get('username111')  #获取用户端发来的cookie
    
        response = render(request,"index.html")
        response = redirect('/index/')
        #设置cookie 10秒后会过期 max_age = 10  截止时间失效
        response.set_cookie('key',"value",max_age = 10)
        import datetime
        current_time = datetime.datetime.utcnow()
        current_time = current_date + datetime.timedelta(seconds=10)
        #expires 具体到哪个时间节点
        response.set_cookie('key',"value", expires = current_time)
        return response
    
    cookie3:    #document.cookie
        request.COOKIES.get('username111')  #获取用户端发来的cookie
    
        response = render(request,"index.html")
        response = redirect('/index/')
        #设置cookie 关闭浏览器失效(还可以设置超时时间)
        #path='/'默认的地址
        #domain=None  生效的域名
        #secure=False  https传输 如果是 要设置为true
        #httponly=True  加上他好一点 在js获取cookie时 获取不到
        response.set_cookie('key',"value")  #关闭浏览器时 才失效
        return response
    
    user_info = {
        'dachengzi':{'pwd':'123'},
        'kanbazi':{'pwd':'321'},
    }
    def login(request):
        if request.method =="GET":
                return render(request,'login.html')
        if request.method == "POST":
            u = request.POST.get('username')
            p = request.POST.get('pwd')
            dic = user_info.get(u)
            if not dic:
                return render(request, 'login.html')
            if dic['pwd'] == p:
                res = redirect('/index/')
                res.set_cookie('username111',u)
                return res
            else:
                return render(request, 'login.html')
    
    def index(request):
        #获取当前已经登录的用户名
        v = request.COOKIES.get('username111')
        if not v:
            return redirect('/login')
    
        return render(request,'index.html',{'current_user':v})
    
        缓存
        中间件
        信号
        CSRF
        Admin
        ModelForm
  • 相关阅读:
    第一篇博文,纪念下
    HDU 1026 Ignatius and the Princess I (bfs+存储路径)
    acer Empowering Technology下载(转)
    设定sql server定期自动备份数据库
    web.config加密解密
    WCF安全性资料
    SharePoint中CAML日期格式
    Map Reduce the Free Lunch is not over?(转)
    asp.net与javascript
    获取当前build的版本信息
  • 原文地址:https://www.cnblogs.com/Liang-jc/p/9196666.html
Copyright © 2011-2022 走看看