登录注册
<form action="" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="password1"><br>
<button type="submit">注册</button>
</form>
<form action="" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<button type="submit">登录</button>
</form>
{{ mes }}
def register(request):
if request.method=='POST':
username=request.POST.get('username')
password=request.POST.get('password')
password1=request.POST.get('password1')
# print(username,password)
user=Teacher(username=username,password=password1)
user.save()
return render(request,'register.html')
def login(request):
mes=''
if request.method=='POST':
username = request.POST.get('username')
password = request.POST.get('password')
u=Teacher.objects.filter(username=username).first()
print(u)
if not all([username,password]):
mes='用户名密码不能为空'
else:
if u:
if password==u.password:
request.session['username']=username
request.session['userid']=u.id
return redirect('/views/index/')
else:
mes='密码错误'
else:
mes='用户名不存在'
return render(request,'login.html',{'mes':mes})