【问题标题】:Python/Flask: ValueError: View function did not return a response [closed]Python / Flask:ValueError:查看函数没有返回响应[关闭]
【发布时间】:2015-11-09 09:26:42
【问题描述】:

尽管阅读了文档和教程,但我无法弄清楚的看似微不足道的问题。继续咳嗽:

builtins.ValueError
ValueError: View function did not return a response

每当我尝试渲染我的模板时。使用 PyCharm 作为编辑器,不会警告任何问题。

website.py:

from flask import Flask, url_for, request, render_template

app = Flask(__name__, template_folder='templates')

@app.route('/')
def hello_world():
    render_template('hello_world.html')

if __name__ == '__main__':

    app.debug = True

    app.run()

hello_world.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello, Flask</title>
</head>
<body>
<h1>Hello, World and Flask!</h1>
</body>
</html>

【问题讨论】:

    标签: python flask


    【解决方案1】:

    当然应该是

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

    在插入 render_template 时忽略了“返回”。

    【讨论】:

      【解决方案2】:

      您需要像这样改进您的 hello_world 方法:

      return render_template(...)

      您看到的错误有点暗示这一点。 Flask 期待返回值,但什么也没有看到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-14
        • 2018-05-16
        • 1970-01-01
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 2015-01-07
        相关资源
        最近更新 更多