1. 获取用户输入内容如下,其中username和password是HTML里面对应的内容
u = request.POST.get('username')
p = request.POST.get('password')
2.重定向的时候需要首先导入redirect包,重定向时候即为
return redirect('https://www.baidu.com/')
3.关于html的部分如下
<h1>用户登录</h1>
<form method="post" action="/login/">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" name="submit" value="登陆"/>
</form>
4.settings代码如下
def login(request):
# print('nihaoya')
# return HttpResponse('<h1>login</h1>')
if request.method == "GET":
return render(request,'login.html')
else:
u = request.POST.get('username')
p = request.POST.get('password')
print(u,p)
if u == 'root' and p == '123':
return redirect('https://www.baidu.com/')
pass
else:
return render(request, 'login.html')
pass
pass
5.关于特殊字符:
HTML里面
{{msg }}
然后在python里面需要对msg进行传参数{'msg':'你好呀'}这样就可以输入一些提示了,比如密码输入错误
return render(request,'login.html',{'msg':'你好呀'})