admin登录
情况1: 登录后会产生一个sessionid
情况2: 自定义设置了key后,会多一个sessionid, 登录后会替换为登录后的sessionid的key值
if username == "maotai" and password == "123456":
request.session['name'] = "maotai-session" #定义一个session key
request.session['name2'] = "maotai2-session"#定义一个session key
自定义sessionid
def login(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
if username == "maotai" and password == "123456":
request.session['name'] = "maotai-session" #定义一个session key
request.session['name2'] = "maotai2-session"#定义一个session key
name = request.session.get("name", "") # 获取一个session key
name2 = request.session.get("name2", "")
# return redirect("/")
return render(request, "index.html", {"name": name, "name2": name2}) #将session key返回到前端
else:
return render(request, "login.html")
elif request.method == "GET":
return render(request, "login.html")
获取sessionid
request.session.get("name", "") # 获取一个session key 获取到的应该是下面的_session里的结果
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:[django]session设置与获取原理 - Python技术站