首页 > 其他分享 >cookie 使用

cookie 使用

时间:2022-12-18 17:11:06浏览次数:33  
标签:get request cookie user 使用 login salt

目录

使用


def index(request):
    # 读cookie
    # is_login = request.COOKIES.get("is_login")
    is_login = request.get_signed_cookie("is_login",salt="12*$543543534534554")
    if is_login == "true":
        user_id = request.get_signed_cookie("user_id",salt="12*$543543534534554") # salt 和设置cookie的时候一致
        username = User.objects.get(pk=user_id).user
        return render(request,"index.html",{"user":username})
    else:
        return redirect("/login/")


def login(request):

    if request.method == "GET":
        return render(request,"login.html")

    else:
        user = request.POST.get("user")
        pwd = request.POST.get("pwd")
        try:
            user = User.objects.get(user=user, pwd=pwd)
            res =  redirect("/index/") # redirect 也是一个返回对象
            # res.set_cookie("is_login","true",max_age=10000) # 只要是返回对象HttpResponse等 都可以设置cookie
            # res.set_cookie("user_id", user.pk,max_age=10000) # 普通使用cookie 不带加密 可以在浏览器串改数据 max_age有效时长
            res.set_signed_cookie("is_login","true",salt="12*$543543534534554") 
            res.set_signed_cookie("user_id", user.pk,salt="12*$543543534534554") # 设置加密cookie,设置一个yan salt指随便设置 设置之后在取的也要加上salt
            return res

        except Exception as e:
            return redirect("/login/")


标签:get,request,cookie,user,使用,login,salt
From: https://www.cnblogs.com/py-zhq/p/16990582.html

相关文章

  • Linux使用yum install报错:Disable the repository, so yum won't use it by default
     已加载插件:fastestmirror,langpacksOneoftheconfiguredrepositoriesfailed(未知),andyumdoesn'thaveenoughcacheddatatocontinue.Atthispointthe......
  • swagger-ui-express 的使用
    swagger-ui-express的使用constswaggerUI=require('swagger-ui-express')constopenapi=require('./openapi.json')app.use('/swagger.html',swaggerUI.serve,s......
  • 如何使用CloudWatch 代理收集指标
    默认情况下,AmazonEC2会将一组与实例相关的指标发送到CloudWatch。比如CPU利用率、磁盘读取和写入指标以及NetworkIn和NetworkOut等指标。但是,EC2不会提供与操作级......
  • 你的项目使用的是哪种配置文件?
    1.开发环境JDK版本:17.0.3IDEA版本:2022.1.4Maven版本:3.8.6SpringBoot版本:3.0.0 2.application.properties配置文件2.1创建Module模块创建spring-boot-prope......
  • Playable API - AnimationClip与AnimatorController放在一起使用
    与这边类似PlayableAPI-简单动画混合-yanghui01-博客园(cnblogs.com),只是跑动画换成了状态机usingUnityEditor.Animations;usingUnityEngine;usingUnityEng......
  • 使用 DirectSound 录制麦克风音频
    使用DirectSound录制麦克风音频本文所有代码均可在以下仓库找到https://gitcode.net/PeaZomboss/learnaudios目录是demo/dscapture之前那篇文章简单介绍了DirectSoun......
  • YARP - 简单使用
    dotnetversion:6.0dotnetnewweb-nMyProxy-fnet6.0dotnetaddpackageYarp.ReverseProxy添加中间件varbuilder=WebApplication.CreateBuilder(args);bu......
  • 使用reduce解决回调
    Q在使用mapbox时在地图上添加很多图片,mapbox需要使用loadImage并在map实例中缓存这些图片。大致的操作模式如下:map.loadImage(imgSrc1,(data,err)=>{if(err)thrower......
  • openwrt开发使用-增加perf
    前言perf性能工具写的文章也有几篇了,这里就不多做介绍了,有兴趣的朋友可以看我之前的几篇文章:《perf性能分析工具使用分享》、《perf补充命令分享》。今天分享的内容是o......
  • ArcObjects SDK开发 017 在ArcObject SDK 中使用Toolbox
    1、Geoprocessor和IGPProcessGeoprocessor是ArcObjectsSDK中定义Tool执行器。IGPProcess接口是ArcObjectsSDK中定义的ArcTool接口。也就是说ArcObjectsSDK定义的ArcToo......