渲染烧瓶HTML表单会导致"UnboundLocalError" [英] Rendering Flask HTML forms results in "UnboundLocalError"
问题描述
我想制作一个Flask应用程序,该应用程序从用户那里接收详细信息.成功注册后,将显示一个新页面,显示输入的详细信息.
I want to make a Flask application that receives the details from a user. After a successful registration, a new page will be displayed showing the entered details.
该应用程序是使用 HTML表单构建的而不是使用 WTForms .
The application is built using HTML forms and not by using WTForms.
以下是app.py的代码:
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)
这是register.html的HTML页面:
<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>
我现在得到的是这个错误:
What I am getting now is this error:
Full Traceback
**UnboundLocalError**
UnboundLocalError: local variable 'result' referenced before assignment.
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/protocol/PycharmProjects/tut/app.py", line 17, in user
return render_template("register.html",result=result)
UnboundLocalError: local variable 'result' referenced before assignment
我尝试打印这些值,但控制台中未显示任何内容,因此我无法纠正此错误.
I tried to print the values, but nothing is shown in the console, so I couldn't rectify this error.
推荐答案
结果
仅在 request
方法为 POST
时存在,如果请求方法是 GET
(这是默认请求之一),也就是说,在提交用户数据表单之前请求页面时, result
将不存在,因此错误消息.另外,您需要将 result
对象呈现到另一个模板中,例如 user.html
:
result
only exists if the request
method is POST
, so if the request method was GET
(which is the default request one), that is when the page is requested before submitting user data form, then result
won't be existing, hence the error message. Plus, you need to render the result
object into another template, for example 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
块中从以下位置进行更改:
Another thing, if the above view function is for register.html
form, then you also need to change in form
block, from:
<form action = "/success" method = "POST">
到
<form action = "/register" method = "POST">
这篇关于渲染烧瓶HTML表单会导致"UnboundLocalError"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!