首页 > 代码库 > 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