首页 > 代码库 > cookies和sessions
cookies和sessions
response 为HttpResponse对象,request为HttpRequest对象
Cookies
设置Cookies response.set_cookie("cookie_key","value")
获取Cookies value = http://www.mamicode.com/request.COOKIES["cookie_key"]
删除Cookies response.delete_cookie("cookie_key",path="/",domain=name)
检测Cookies if "cookie_name" is request.COOKIES
例子:
def test_cookies(request): if "test" in request.COOKIES: temp=request.COOKIES[‘test‘] return HttpResponse("cookies="+temp) else: response = HttpResponse("test") response.set_cookie("test","123456789") return response
Sessions
设置Sessions值 request.session[‘session_name‘] ="admin"
获取Sessions值 session_name = request.session["session_name"]
删除Sessions值 del request.session["session_name"]
检测是否操作session值 if "session_name" is request.session :
def test_sessions(request): if "test" in request.session: temp=request.session[‘test‘] return HttpResponse("session="+temp) else: request.session[‘test‘]=‘123456789‘ response = HttpResponse("abcdef") return response
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。