1.cookie设置
if userResult and userResult.is_active:
response=redirect(to='/')
response.set_cookie("username",username,max_age=3600)
context["username"]=username
return response
2.cookie删除
try:
response=redirect(to='/account/login')
response.delete_cookie('username')
return response
except:
return render(request,'index.html',context)
cookie获取
def index(request):
context={}
username=request.COOKIES.get("username",None)
#或者username=request.COOKIES["username"]
context["username"]=username
return render(request,"index.html",context)
session的设置
request.session["username"]=username
session的获取
username=request.session['username']
或者username=request.session.get('username','')
session的删除
del request.session['username']