先修改index.html文件(一个包含用户名与登录密码的表单)
<!DOCTYPE html>
<html lang="en">
<body>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
background: pink;
}</style>
<h1 style="...">用户输入</h1>
<from action="/index/" method="post">
<input type="text" name="username"/>
<input type="password" name ="password"/>
<input type ="submit" value="提交"/>
</from>
</body>
</html>
然后修改views.py文件
def index(request):
if request.method == "POST":
username = request.POST.get("username", None),
password = request.POST.get("password", None),
print(username, password)
return render(request, "index.html",)
运行结果:
关于request.method == “POST”: 表示提交请求使用的HTTP方法是POST
关于POST:一个类字典对象,包含所有的HTTP的POST参数的信息。见 QueryDict文档。
[关于python request属性及方法介绍]
https://www.cnblogs.com/testyao/p/5698714.html