render_template有问题吗?

时间:2018-09-29 16:19:50

标签: python html flask

我是flask的新手(但不是Python的新手),并且我一直在关注Corey Schafer的YouTube教程中的设置步骤。我几乎完成了Corey在视频中所做的所有工作。我正在Mac而不是Sublime上使用Visual Studio Code,这是唯一的区别。

当我进入使用render_template函数执行基本html文件的步骤时,出现了问题,并且出现了各种错误。我想念什么?

我的flask_blog.py程序:

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

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

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

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

home.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>Home Page</h1>
</body>
</html>

about.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>About Page</h1>
</body>
</html>

这是我尝试导航至主页时得到的信息: Screenshot 1/2

Screenshot 2/2

1 个答案:

答案 0 :(得分:2)

确保将html文件存储在名为templates的子目录中:

parentfolder
  templates
     home.html
     about.html
  flask_blog.py \\this is the file that contains the route declarations