zoukankan      html  css  js  c++  java
  • django-cookie-session

    cookie 设置 读取 加密 删除

    #设置cook
    def seecook(request):
        response =HttpResponse('cook设置成功')
        response.set_cookie('cookiekey','setcook1',max_age=600)
        return response
    #读取cook
    def readcook(request):
    
        name=request.COOKIES.get('cookiekey')
        return HttpResponse('cook读取成功:{0}'.format(name))
    
    #设置加密cook(set)
    def setmicook(request):
        response=HttpResponse('加密cook设置成功')
        response.set_signed_cookie('jiamicookie-key','jiamicookie-value',salt='python ok')
        return response
    
    #读取加密cook(get)
    def readmicook(request):
        name=request.get_signed_cookie('jiamicookie-key',salt='python ok')
        return HttpResponse('获取到的加密cook是:{0}'.format(name))
    
    #删除cook
    def deletecook(request):
        response=HttpResponse('cookie删除成功!')
        response.delete_cookie('jiamicookie-key')
    return response

    cookie 记录账户名

    HTML

    <input type="text" name="zhanghao" value="{{ zhanghao }}">

    views

    #登录成功 cookie记录账号名 
    def denglu(request):
        zhanghao=request.POST.get("zhanghao")
        mima=request.POST.get("mima")
    if zhanghao=='admin' and mima=='123':
    #验证成功 重定向地址
            response=HttpResponseRedirect('/zhuye/')
            response.set_signed_cookie("zhanghao",zhanghao,salt='python')
            response.set_signed_cookie("mima", mima, salt='python')
            return response
        else:
            try:
                zhanghao=request.get_signed_cookie("zhanghao",salt='python')
                return render(request,'login.html',{'zhanghao':zhanghao})
            except:
    
                return render(request,'login.html')

    cookie登录成功记录账号

    Session 登录成功记录账号 显示在主页

    <form method="post" action="/denglu/">
        {% csrf_token %}
        <input type="text" name="zhanghao" value="{{ zhanghao }}">
        <input type="password" name="mima">
        <button>登录</button>
    </form>
    ****************************************************
    {{ names }}的主页
    <a href="/tuichu/">退出</a>
    def login(request):
        try:
            # 提取cookie账号
            zhanghao = request.get_signed_cookie("zhanghao", salt='python')
            return render(request, 'login.html', {'zhanghao': zhanghao})
        except:
            return render(request,'login.html')
    
    def denglu(request):
        zhanghao=request.POST.get('zhanghao')
        mima = request.POST.get('mima')
        if zhanghao=='admin' and mima=='123':
            # 登录成功写入session
            request.session['names'] = zhanghao
            # 登录成功写入cookie
            response=HttpResponseRedirect('/zhuye/')
            response.set_signed_cookie("zhanghao", zhanghao, salt='python')
            return response
        else:
            return HttpResponseRedirect('/login/')
    
    def zhuye(request):
        # 登录成功主页提取session
        rec = request.session.get('names')
        if rec=='':
            return HttpResponseRedirect('/login/')
        else:
            return render(request,'zhuye.html',{'names':rec})
    
    def tuichu(request):
        try:
            #退出删除session
            del request.session['names']
            return HttpResponseRedirect('/login/')
        except:
            return HttpResponseRedirect('/login/')
            pass

     

    文件上传

    <form method="post" enctype="multipart/form-data" action="/shangchuan/">
        {% csrf_token %}
        <input type="file" name="files">
        <button type="submit">上传</button>
    </form>
    def wenjian(request):
        return render(request,'wenjian.html')
    def shangchuan(request):
        files=request.FILES.get('files')
        filedname=files.name
        print('
    文件上传信息')
        print('文件名称:{0}'.format(filedname))
        print('文件大小:{0}'.format(files.size))
        print('文件类型:{0}'.format(filedname.split('.')[-1]))
        print('*'*40)
        loadpath="app/static/file"
        if not os.path.exists(loadpath):
            os.mkdir(loadpath)
            print('文件夹首次创建成功')
        fileloadpath=loadpath+os.sep+filedname
        print('【系统】文件路径:{0}'.format(fileloadpath))
        try:
            with open(fileloadpath,'wb+') as fd:
                for chunk in files.chunks():
                    fd.write(chunk)
            print('【系统】文件写入成功')
            return HttpResponse("<script> alert('文件上传成功');location.herf='/wenjian/' </script>")
        except:
            return HttpResponse("<script> alert('文件上传失败');location.href='/wenjian/'</script>")

     

    文件类型判断

    上传照片回显

    <head>
        <meta charset="UTF-8">
        {% load static %}
        <title>文件</title>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data" action="/shangchuan/">
        {% csrf_token %}
        <input type="file" name="files"><br>
        <input type="file" name="files">
        <button type="submit">上传</button>
        {{ message }}
    </form>
    {% if imgurl %}
    <img src="{%  static imgurl %}">
    {% endif %}
    
    
    </body>
    def wenjian(request):
        return render(request,'wenjian.html')
    def shangchuan(request):
        jiance=True
    
        fileslist=request.FILES.getlist('files')
        for files in fileslist:
    
            filedname = files.name
            filedtype=filedname.split('.')[-1]
            print('
    文件上传信息')
            print('文件名称:{0}'.format(filedname))
            print('文件大小:{0}'.format(files.size))
            print('文件类型:{0}'.format(filedname.split('.')[-1]))
            print('*' * 40)
            imgtypelist=['jpg','png','gif','bmp']
            loadpath = "app/static/file"
            if filedtype not in imgtypelist:
                return render(request,'wenjian.html',{'message':'文件类型不符'})
    
            if not os.path.exists(loadpath):
                os.mkdir(loadpath)
                print('文件夹首次创建成功')
            imgpre=str(uuid.uuid1())
            filedname=imgpre+'.'+filedtype
            fileloadpath = loadpath + os.sep + filedname
            print('【系统】文件路径:{0}'.format(fileloadpath))
            try:
                with open(fileloadpath, 'wb+') as fd:
                    for chunk in files.chunks():
                        fd.write(chunk)
                print('【系统】文件写入成功')
                #return HttpResponse("<script> alert('文件上传成功');location.herf='/wenjian/' </script>")
                content=dict()
                content['message']='图片上传成功'
                content['imgurl']='file/'+filedname
                return render(request,'wenjian.html',content)
            except:
                jiance=False
  • 相关阅读:
    解决ubuntu不能安装g++的问题
    解决VMware虚拟机不能上网的问题
    打开vmvare出现The VMware Authorization Service is not running。
    word2-寻找社交新浪微博中的目标用户
    新浪云计算SAE部署代码过程
    Python如何调用新浪api接口的问题
    work1-英语辅导班在线报名系统
    Mysql对自增主键ID进行重新排序
    如何使用LIBSVM,从安装到基本实例使用
    laravel怎么创建一个简单的blog
  • 原文地址:https://www.cnblogs.com/huazhou695/p/10008264.html
Copyright © 2011-2022 走看看