今天使用Python做request测试的时候,运行出项目后,输入views模块注册的path,发现页面不跳转,如下:
可以看到提示已经很明确了,路径不匹配,随即我检查了下urls.py、views.py、login.html如下:
# 登录界面
path('login', views.login),
def login(request):
if request.method == "GET":
return render(request, "login.html")
# 如果是POST请求,获取用户提交的数据
# print(request.POST)
username = request.POST.get("user")
password = request.POST.get("pwd")
if username == 'root' and password == "123":
# return HttpResponse("登录成功")
return redirect("http://www.chinaunicom.com.cn/")
# return HttpResponse("登录失败")
return render(request, 'login.html', {"error_msg": "用户名或密码错误"})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login/">
{% csrf_token %}
<input type="text" name="user" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<input type="submit" value="提交"/>
<span style="color: red;">{{ error_msg }}</span>
</form>
</body>
</html>
发现urls.py中login的注册少了“/”
改为如下:
# 登录界面
path('login/', views.login),
重启页面,跳转成功