You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署Flask应用遇500错误:base.html模板未找到

Flask部署Heroku遇500错误:TemplateNotFound: base.html

问题详情

已按教程创建requirements.txt和Procfile,Heroku部署成功但访问时返回500内部服务器错误,本地运行完全正常。Heroku日志核心错误为jinja2.exceptions.TemplateNotFound: base.html,base.html与其他模板均存放于templates文件夹内,尝试过修改模板语法(如引号格式),本地正常但Heroku依旧报错。

相关文件内容

requirements.txt

bcrypt==3.2.2
certifi==2022.6.15
cffi==1.15.1
charset-normalizer==2.1.0
click==8.1.3
colorama==0.4.5
dnspython==2.2.1
email-validator==1.2.1
Flask==2.2.2
Flask-Bcrypt==1.0.1
Flask-Login==0.6.2
Flask-SQLAlchemy==2.5.1
Flask-WTF==1.0.1
greenlet==1.1.2
gunicorn==20.1.0
idna==3.3
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
pycparser==2.21
requests==2.28.1
SQLAlchemy==1.4.40
tailwind==3.1.5b0
tailwindcss==0.0.1
urllib3==1.26.11
Werkzeug==2.2.2
WTForms==3.0.1

Procfile

web: gunicorn main:app

(尝试过添加/移除.txt后缀,无变化)

首页路由代码(main.py)

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

index.html代码

{%extends "base.html"%}

Heroku核心错误日志

jinja2.exceptions.TemplateNotFound: base.html

解决方案

  • 检查文件名大小写:Heroku运行在Linux系统上,文件名区分大小写。本地Windows/macOS可能不区分,若你本地的模板文件名为Base.html或其他大小写组合,需统一改为base.html,并确保Git提交的是正确大小写的文件。
  • 显式指定Flask模板目录:若main.py不在项目根目录,初始化Flask应用时需明确指定模板文件夹路径:
    app = Flask(__name__, template_folder='templates')
    
    确保路径与实际文件夹位置匹配(若templates在子目录,需调整路径如'../templates')。
  • 确认Git追踪模板文件:运行git status检查templates文件夹及内部文件是否已被Git追踪,未追踪的话执行git add templates/,然后提交并推送到Heroku:
    git commit -m "Add templates folder"
    git push heroku main
    
  • 清除Heroku缓存:缓存可能导致旧文件残留,执行以下命令清除缓存后重新部署:
    heroku repo:purge_cache -a 你的应用名称
    git push heroku main
    

内容的提问来源于stack exchange,提问作者Leo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:45:56