一、session介绍
session默认可存在于表django_session中,类似于cookie,它是将值存储在服务端上,表中字段有session_key、session_data、expire_date
二、session示例
- views.py
def login_session(request):
if request.method == "GET":
return render(request,'login.html')
else:
user = request.POST.get("user")
pwd = request.POST.get("pwd")
user_obj = UserInfo.objects.filter(name=user,pwd=pwd).first()
if user_obj:
request.session["is_login"] = True
request.session["user_name"] = user
print(request.session["user_name"])
obj = HttpResponse("登陆成功")
return obj
return HttpResponse('Error!')
def index_session(request):
user = request.session.get("user_name")
print('>>>',request.session.get("is_login"))
print('>>>',request.session.get("user_name"))
if request.session.get("is_login"):
return render(request,'index.html',locals())
else:
return render(request, 'login.html',locals())
def loginout_session(request):
print('>>>>',request.session.get("is_login"))
print('>>>>',request.session.get("user_name"))
request.session.flush()
return redirect('/login_session/')
备注:包含三个函数分别是登录、验证、退出
留言