部署到 Heroku 后找不到模板

Template not found after deploying to Heroku

阅读 this 问题后,我对为什么在部署到 Heroku 后尝试启动我的应用程序时出现以下错误有了一些了解:raise TemplateNotFound(template), jinja2.exceptions.TemplateNotFound: index.html.

他们说 templates 应该在项目目录根目录下 here。无论如何我可以修改 Heroku 试图寻找 templates 目录的位置吗?我修改了我的项目结构,因为我的应用程序越来越大,开始看起来有点杂乱无章。

下面是我当前的项目结构:

Project
│
├── App
│   ├── DbMs
│   │   ├── db_actions.py
│   │   └── user_operations.py
│   ├── UI
│   │   ├── static
│   │   │   ├── css
│   │   │   │   └── main.css
│   │   │   ├── fonts
│   │   │   │   └── Gotham_Medium_Regular.json
│   │   │   ├── images
│   │   │   │   ├── favicons
│   │   │   │   │   ├── android-chrome-192x192.png
│   │   │   │   │   ├── android-chrome-512x512.png
│   │   │   │   │   ├── apple-touch-icon.png
│   │   │   │   │   ├── favicon-16x16.png
│   │   │   │   │   ├── favicon-32x32.png
│   │   │   │   │   ├── favicon.ico
│   │   │   │   │   └── site.webmanifest
│   │   │   │   ├── header.jpeg
│   │   │   │   ├── logo.png
│   │   │   │   └── radar-chart.png
│   │   │   └── javascript
│   │   │       ├── FontLoader.js
│   │   │       ├── TextGeometry.js
│   │   │       ├── create.js
│   │   │       ├── exploreMusic.js
│   │   │       └── tracks.js
│   │   └── templates
│   │       ├── base.html
│   │       ├── create.html
│   │       ├── index.html
│   │       ├── information.html
│   │       └── tracks.html
│   ├── __init__.py
│   ├── authenticate.py
│   ├── routes.py
│   ├── services.py
│   └── templates
├── Pipfile
├── Procfile
├── README.md
├── Testing
│   ├── __init__.py
│   └── testing.py
├── __pycache__
│   ├── authenticate.cpython-39.pyc
│   ├── config.cpython-39.pyc
│   ├── main.cpython-39.pyc
│   ├── services.cpython-39.pyc
│   └── user_operations.cpython-39.pyc
├── config.py
├── package-lock.json
├── package.json
├── requirements.txt
├── run.py
└── setup.py

有关更多上下文,我在本地 运行 一切都很好,我只在部署后看到此错误。感谢任何可以提供见解的人。

编辑:我尝试将 templates 文件夹移动到 App 目录以及项目根目录,并两次更改 __init__.py 中的以下行:

template_dir = os.path.abspath('../Project/templates/')

每次部署后,我更改了templates的位置,仍然看到来自Heroku的相同错误:raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html

原来 Heroku 对修改 templatesstatic 文件夹的位置很挑剔。

关于这个问题的误导是,我可以 运行 在本地一切正常,但是一旦我部署到 Heroku,整个应用程序就会崩溃..

我解决这个问题的方法是将 templates & static 移动到 App 目录中。我还必须确保我更改了以下行:

template_dir = os.path.abspath('../Soulify/App/templates/')
static_dir = os.path.abspath('../Soulify/App/UI/static/')
app = Flask(__name__, template_folder=template_dir, static_folder=static_dir)

至:

app = Flask(__name__)

另请注意,模板文件夹必须准确命名 templates,否则 Heroku 将失败。再一次,很烦人。