Python 呈现HTML表单时会导致;“未绑定本地错误”;
我想制作一个Flask应用程序,从用户那里接收详细信息。成功注册后,将显示一个新页面,显示输入的详细信息 应用程序是使用而不是使用创建的 以下是app.py的代码:Python 呈现HTML表单时会导致;“未绑定本地错误”;,python,flask,Python,Flask,我想制作一个Flask应用程序,从用户那里接收详细信息。成功注册后,将显示一个新页面,显示输入的详细信息 应用程序是使用而不是使用创建的 以下是app.py的代码: from flask import Flask, render_template, request app = Flask(__name__) @app.route('/register',methods = ['POST', 'GET']) def user(): if request.method == 'POST'
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/register',methods = ['POST', 'GET'])
def user():
if request.method == 'POST':
result = request.form
name = result['name']
phoneno = result['phoneno']
email = result['email']
password = result['password']
if name or phoneno or email or password is not None:
print name,phoneno,email,password
return render_template("register.html",result=result)
if __name__ == '__main__':
app.run(debug = True)
<html>
<body>
<form action = "/success" method = "POST">
<p>Name <input type = "text" name = "name" /></p>
<p>Phone no <input type = "text" name = "phoneno" /></p>
<p>Email <input type = "text" name = "email" /></p>
<p>Password <input type ="text" name = "password" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
这是register.HTML的HTML页面:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/register',methods = ['POST', 'GET'])
def user():
if request.method == 'POST':
result = request.form
name = result['name']
phoneno = result['phoneno']
email = result['email']
password = result['password']
if name or phoneno or email or password is not None:
print name,phoneno,email,password
return render_template("register.html",result=result)
if __name__ == '__main__':
app.run(debug = True)
<html>
<body>
<form action = "/success" method = "POST">
<p>Name <input type = "text" name = "name" /></p>
<p>Phone no <input type = "text" name = "phoneno" /></p>
<p>Email <input type = "text" name = "email" /></p>
<p>Password <input type ="text" name = "password" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
我试图打印值,但控制台中没有显示任何内容,因此无法纠正此错误。
结果
仅在请求
方法为发布
时存在,因此如果请求方法为获取
(这是默认请求),即在提交用户数据表单之前请求页面时,然后,result
将不存在,因此会显示错误消息。另外,您需要将结果
对象呈现到另一个模板中,例如user.html
:
@app.route('/register',methods = ['POST', 'GET'])
def user():
if request.method == 'POST':
result = request.form
name = result['name']
phoneno = result['phoneno']
email = result['email']
password = result['password']
if name or phoneno or email or password is not None:
print name,phoneno,email,password
return render_template("user.html",result=result)
return render_template("register.html")
另一件事,如果上面的视图函数用于register.html
form,那么您还需要在form
block中更改,从:
<form action = "/success" method = "POST">
到
结果
仅当请求
方法为POST
时才存在,因此如果请求方法为GET
(这是默认请求方法),即在提交用户数据表单之前请求页面时,结果
将不存在,因此会出现错误消息。另外,您需要将结果
对象呈现到另一个模板中,例如user.html
:
@app.route('/register',methods = ['POST', 'GET'])
def user():
if request.method == 'POST':
result = request.form
name = result['name']
phoneno = result['phoneno']
email = result['email']
password = result['password']
if name or phoneno or email or password is not None:
print name,phoneno,email,password
return render_template("user.html",result=result)
return render_template("register.html")
另一件事,如果上面的视图函数用于register.html
form,那么您还需要在form
block中更改,从:
<form action = "/success" method = "POST">
到
试试这个:app.py new.html
{% for k,v in results.iteritems() %}
<p>{{v}}</p>
{% endfor %}
{%results.iteritems()中的k,v为%
{{v}
{%endfor%}
register.html与您的相同。您缺少用于显示表单输入数据的html模板,new.html文件将呈现您在表单中输入的值。请尝试以下操作:
app.py new.html
{% for k,v in results.iteritems() %}
<p>{{v}}</p>
{% endfor %}
{%results.iteritems()中的k,v为%
{{v}
{%endfor%}
register.html与您的相同。您缺少用于显示表单输入数据的html模板,new.html文件将呈现您在表单中输入的值。这使我相信您在此处的缩进与实际文件中的缩进不匹配。表单的内容类型是什么?@dim您能再次检查app.py吗?@roy我不明白您在此处所说的内容是什么内容类型没有添加到表单中。有必要添加内容类型吗?这让我相信你在这里的缩进与你实际文件中的缩进不匹配。表单的内容类型是什么?@dim你能再检查一遍app.py吗?@roy我不明白你在这里的意思内容类型没有添加到表单中。是否需要添加内容类型?@roy感谢您的支持:)@Protocol…sure:)@roy感谢您的支持:)@Protocol…sure:)