def login_action(request):
if request.method == 'POST':
username = request.POST.get('username','')
password = request.POST.get('password','')
user = auth.authenticate(username=username,password=password)
if user is not None:
auth.login(request,user)
# 登录保存session
request.session['user'] = username
response = HttpResponseRedirect('/event_manage/')
# 设置cookie,3600为保持时间,默认为秒
response.set_cookie('user',username,3600)
return response
else:
return render(request, 'index.html', {'error':'账号或密码错误!'})
# 获取浏览器session
username = request.session.get('user','')
# 获取浏览器cookies
username = request.COOKIES.get('user','')
此外还需要创建django_session表,存放用户session_id
cmd.exe执行以下命令:
python manage.py migrate