首页 > 其他 > 详细

cookies和sessions

时间:2014-05-16 00:58:48      阅读:418      评论:0      收藏:0      [点我收藏+]

response 为HttpResponse对象,request为HttpRequest对象

Cookies

    设置Cookies     response.set_cookie("cookie_key","value")

    获取Cookies  value = request.COOKIES["cookie_key"]
    删除Cookies  response.delete_cookie("cookie_key",path="/",domain=name)
   检测Cookies    if "cookie_name" is request.COOKIES

例子:

bubuko.com,布布扣
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
bubuko.com,布布扣

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 :

 

bubuko.com,布布扣
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
bubuko.com,布布扣

cookies和sessions,布布扣,bubuko.com

cookies和sessions

原文:http://www.cnblogs.com/sklww/p/3730136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!