Flask项目中index.html模板存在却报TemplateNotFound错误的问题求助
Flask项目中index.html模板存在却报TemplateNotFound错误的问题求助
我正在用Flask做一个学校项目的Web应用,但一直卡在一个问题上——Flask死活找不到index.html模板。我反复确认过文件夹结构是对的,模板也确实放在了对应的templates文件夹里,但就是不行。
我的文件夹结构是这样的:
oshinosensei/ ├── static/ │ ├── Lorem.jpg │ ├── Ipsum.jpg │ ├── ... ├── templates/ │ ├── index.html │ ├── result.html └── app.py
明明结构没问题,可我运行python app.py之后,服务器倒是能正常启动,但页面加载失败,弹出这个错误:
jinja2.exceptions.TemplateNotFound: index.html
我已经确认过index.html确实在templates文件夹里,还特意在app.py里加了代码打印当前工作目录:
import os print("Current working directory:", os.getcwd())
输出显示工作目录是对的:
Current working directory: C:\Users\남경천\oshinosensei
我的app.py代码是这样的:
import os from flask import Flask, render_template, request app = Flask(__name__, template_folder='templates') @app.route("/") def index(): return render_template("index.html") if __name__ == "__main__": app.run(debug=True)
我甚至尝试在初始化Flask的时候手动指定template_folder='templates'参数,但还是报同样的错误。
有没有大佬能给我点建议或者帮忙排查一下问题呀?万分感谢!
备注:内容来源于stack exchange,提问作者user28572487
相关产品推荐
相关产品推荐

