Flask HTML forms- UnboundLocalError

时间:2016-07-30 15:08:01

标签: python flask

我是Flask应用程序开发的新手。我想创建一个Flask应用程序来接收用户的详细信息。成功注册后,将显示一个新页面,显示输入的详细信息。该应用程序是使用HTML forms构建的,而不是使用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>

我现在得到的是一个错误:

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

我试图打印这些值,但控制台中没有显示任何内容,因此我无法纠正此错误。提前谢谢!

2 个答案:

答案 0 :(得分:2)

result仅在request方法为POST时才存在,因此如果请求方法为GET(这是默认请求方法),那么就是页面在提交用户数据表单之前请求,然后result将不存在,因此出现错误消息。另外,您需要将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 action = "/success" method = "POST">

<form action = "/register" method = "POST">

答案 1 :(得分:0)

试试这个:
app.py

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def form():
    return render_template('register.html')


@app.route('/success',methods = ['POST', 'GET'])
def user():
    if request.method == 'POST':
        results = request.form
    return render_template('new.html',results=results)

if __name__ == '__main__':
    app.run(debug = True)

new.html

{% for k,v in results.iteritems() %}
    <p>{{v}}</p>
{% endfor %}

register.html与您的相同。
您缺少用于显示表单输入数据的html模板,new.html文件将呈现您在表单中输入的值。