Django中的cookie与session操作实例代码

添加cookie:

rush:py;"> 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:

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

删除cookie:

rush:py;"> Response.delete_cookie("username")

添加session:

rush:py;"> 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:

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

删除session:

rush:py;"> del req.session['username']

总结

以上所述是小编给大家介绍的Django中的cookie与session操作实例代码,希望对大家有所帮助。程序员遇到问题都会上(编程之家jb51.cc)查找问题解答方法!如果觉得站点还不错,随手转发给程序员朋友一下!

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...