为什么这个简单的应用会返回 500 错误?

Why is this simple app returning a 500 error?

我收到一个内部服务器错误,我不确定是什么问题。 index.html 与 Python 文件位于同一目录中。

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def hello_world():
    author = "Rick"
    name = "1st flask app"
    return render_template('index.html', author=author, name=name)

if __name__ == "__main__":
    app.run()
<!DOCTYPE html>
<html>
  <head>
    <title>{{ author }}'s app</title>
  </head>

  <body>
    <h2>Hello {{ name }}!</h2>
    <p>Please show up on the page</p>
  </body>
</html>

为什么我收到 500 错误而不是我的渲染模板?

创建一个名为 templates 的目录并将 index.html 放入其中。

您可以在调试模式下通过运行获得更多关于错误的信息:

app.run(debug=True)

当您创建应用时 app = Flask(__name__) 您还可以包含 template_folder 参数来指定包含您的模板的文件夹。如果您不这样做,render_template 将在名为 templates 的默认文件夹中查找它们。如果这样的文件夹不存在,或者如果在其中找不到您的模板,您将收到内部服务器错误 500 而不是您呈现的模板。