def login(request):
if request.method == 'GET':
form = account.LoginForm()
return render(request, 'login.html',{'form':form })
else:
form = account.LoginForm(request.POST)
result = {'status': False, 'message': None, 'errors': None}
if form.is_valid():
name = form.cleaned_data['username']
pwd = form.cleaned_data['password']
userInfo = models.UserInfo.objects.filter(name=name,password=pwd).first()
if userInfo:
request.session['UserInfo'] = name
return redirect('/')
else:
result['errors'] = '用户名或密码错误' # 这里是不兼容的地方,这个错误html不知道怎么显示给用户
return render(request, 'login.html', {'form': form})
else:
return render(request, 'register.html', {'form': form})
def logout(request):
request.session.clear()
return redirect('/')
def index(request):
// sessionid 未登录就已经有,因为进行http请求就是一次会话,未登录,session也有其它值在记录着
username = request.session.get('UserInfo') // 直接拿。不需要从cookies中拿sessionid,再拿session,底层已经做好
return render(request, 'index.html', {'username':username})
标签:username,return,登录,form,request,html,二十九,session,相关
From: https://www.cnblogs.com/zhlforhe/p/18013996