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

部署至Heroku后出现TemplateDoesNotExist错误,本地运行正常

Django部署Heroku后出现TemplateDoesNotExist错误排查

问题背景

本地使用生产环境配置和数据库运行Django应用一切正常,但部署到Heroku后,访问应用时出现模板找不到的错误。错误提示系统尝试查找index.html和fund/fund_list.html,但项目中根本不存在fund/fund_list.html文件,且通过搜索未找到任何代码对该文件的引用。

项目结构

root folder
 env
 src
  app
  fund
  static
  staticfiles
  templates
   includes
    funds_table.html
    pagination.html
   about_me.html
   base.html
   index.html
  user_account
  manage.py
 requirements.txt
 Procfile
 .gitignore

模板配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR), 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

已确认os.path.join(BASE_DIR)返回正确路径project_root/src。

错误日志

raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: index.html, fund/fund_list.html
"GET / HTTP/1.1" 500 145 "https://dashboard.heroku.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"

排查方向

  • 检查fund应用下的视图代码:如果使用了**基于类的视图(如ListView)**但未显式指定template_name,Django会自动尝试加载<app_name>/<model_name>_list.html。若fund app中有Fund模型,就会触发查找fund/fund_list.html的逻辑。
  • 确认Git提交完整性:检查index.html是否被提交到Git仓库,避免被.gitignore规则排除。Heroku仅部署Git仓库内的文件,未提交的模板会无法被找到。
  • 查看Heroku容器内的文件结构:通过命令heroku run bash进入应用容器,检查src/templates目录下是否存在index.html,同时确认fund app目录下是否有意外生成的模板相关文件。
  • 检查生产环境配置:确认settings.py的生产环境部分是否修改了TEMPLATES的DIRS配置,导致模板路径错误。
  • 清理Heroku缓存:运行heroku repo:purge_cache清除旧缓存后重新部署,避免缓存引发的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 14:40:34