Django中Cookie使用

读取Cookie

if "UserName" in request.COOKIES: # 判断网站中是否存在

Cookie data["UserName"] = request.COOKIES["UserName"] # 读取网站Cookie

设置Cookie与删除Cookie

obj = redirect("/Index") # 设置跳转路径

if "remName" in request.POST:

    expiration = datetime.now() + timedelta(days=30) # 设置Cookie 30天过期

    # set_cookie(key,val,expires,path,domain,secure,httponly)

    # 设置Cookie值和过期时间

    obj.set_cookie("UserName", request.POST["UserID"], expires=expiration, path='/', domain=None, secure=False, httponly=False)

else:

    obj.delete_cookie("UserName") # 删除Cookie

return obj

你可能感兴趣的:(Django中Cookie使用)