这篇文章给大家介绍cookie与session怎么在Django中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
添加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']
关于cookie与session怎么在Django中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。