python - Flask Forms if 语句返回

标签 python flask

我正在构建一个 flask 应用程序,当我创建一个 fee_handler 路由时,我一直收到错误消息,即使我过去构建过这样的应用程序也是如此。 我不确定这里有什么问题。

@app.route('/handle_form', methods=['POST', 'GET'])
def fee_handler():
    if request.method == "POST":
        x = request.form['x']
        y = request.form['y']
        z = request.form['z']
        a = request.form['a']
        fee.feeCreation(x, y, z, a)
        return render_template('result.html', x=x, y=y, z=z, a=a)

TypeError: View 函数未返回有效响应。该函数要么返回 None,要么在没有返回语句的情况下结束。

如果我将返回移动到与 if 一致的位置,我会得到一个局部变量错误。

最佳答案

在这种情况下,最好创建两个路由:一个用于提供表单,另一个用于接收值并将它们呈现给用户:

@app.route('/enter_values', methods=['GET'])
def enter_values():
   return flask.render_template('form_html.html')

@app.route('/handle_form', methods=['POST'])
def fee_handler():
  x = request.form['x']
  y = request.form['y']
  z = request.form['z']
  a = request.form['a']
  fee.feeCreation(x, y, z, a)
  return render_template('result.html', x=x, y=y, z=z, a=a)

form_html.html模板中,确保表单action参数指向/handle_form:

<form method='POST' action = '/handle_form'>
  ...
</form>

关于python - Flask Forms if 语句返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824723/

相关文章:

python - 在 Python Flask 中维护 session 数据

azure - Azure上的Docker compose webapp无法访问

python - 套接字错误 :[errno 99] cannot assign requested address : flask and python

python - 如何在 Python numpy 中的二维数组上应用一维掩码?

python re.findall 解析十六进制转储文件中签名数据之间的数据

python - 带有数组的函数的 PyCharm getitem 警告

python - {%block content%} 内 flask 中的新行

从 bash 运行的 python 多行命令

python - Blender - 移动网格,使最小 Z 点位于 Z = 0 平面上

python-3.x - 我们如何使用 Flask 对 SQL Server 搜索结果进行分页