python - 渲染 Flask HTML 表单结果为 "UnboundLocalError"

标签 python 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

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

最佳答案

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")

另外,如果上面的 View 函数是针对register.html表单的,那么你还需要在form block 中进行更改,来自:

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

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

关于python - 渲染 Flask HTML 表单结果为 "UnboundLocalError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675377/

相关文章:

python - Unicode解码错误: 'ascii' codec can't decode byte 0xe2 in position 35: ordinal not in range(128)

python - sqlalchemy 似乎不喜欢 __getattr__()

python - uWSGI touch-reload 选项不起作用

python - 如何在 python turtle 模块中使 Canvas 变大

Python 2 正整数列表查找素数

python - Python中如何处理带有参数的异常

javascript - 如何访问 bootstrap-slider 的值(使用 Flask)

python - AttributeError : 'NoneType' object has no attribute 'groups' - re. 搜索

python - 通过 GAE header 问题发布请求

javascript - 从html中获取数据<对其进行一些操作>并使用ajax或js将数据传回前端