我试图渲染文件home.html。该文件存在于我的项目中,但我一直得到jinja2.exceptions。TemplateNotFound: home.html当我试图渲染它。为什么弗拉斯克找不到我的模板?
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
/myproject
app.py
home.html
我认为Flask默认使用目录模板。你的代码应该是这样的
假设这是你的hello.py
from flask import Flask,render_template
app=Flask(__name__,template_folder='template')
@app.route("/")
def home():
return render_template('home.html')
@app.route("/about/")
def about():
return render_template('about.html')
if __name__=="__main__":
app.run(debug=True)
你的工作空间结构就像
project/
hello.py
template/
home.html
about.html
static/
js/
main.js
css/
main.css
此外,您还创建了两个HTML文件,名称为home.html和about.html,并将这些文件放在模板文件夹中。