欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django中的cookie與session操作實(shí)例代碼

 更新時(shí)間:2017年08月17日 16:38:28   作者:Y黃河R  
本文通過(guò)示例代碼給大家介紹了Django中的cookie與session操作,需要的朋友參考下吧

添加cookie:

def login(req):
  if req.method=="POST":
    uf = UserInfoForm(req.POST)
    if uf.is_valid():
      username = uf.cleaned_data["username"]
      password = uf.cleaned_data["password"]
      print username,password
      users = UserInfo.objects.filter(username=username,password=password)
      if users:
        response = HttpResponseRedirect("/index/")
        response.set_cookie("username",username,3600)
        return response
      else:
        return HttpResponseRedirect("/login")
      # return HttpResponseRedirect()
  else:
    uf = UserInfoForm()
  return render_to_response("login.html",{"uf":uf})

獲得cookie:

def index(req):
  username = req.COOKIES.get("username","")return render_to_response("index.html",{"username":username})

刪除cookie:

  Response.delete_cookie("username")

添加session:

def sesion(req):
  if req.method == "POST":
    uf = UserInfoForm(req.POST)
    if uf.is_valid():
      username = uf.cleaned_data["username"]
      req.session["username"] = username
      return HttpResponseRedirect("/index/")
  else:
    uf = UserInfoForm()
  return render_to_response("LoadFile.html",{"uf":uf})

獲取session:

def index(req):
  username = req.session.get("username","")
  return render_to_response("index.html",{"username":username})

刪除session:

del req.session['username']

總結(jié)

以上所述是小編給大家介紹的Django中的cookie與session操作實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論