在Heroku上部署Flask应用,但未找到/ [GET]和模板的异常

时间:2019-07-09 10:53:04

标签: python heroku flask

我正在尝试在Heroku上部署我的应用程序,但它在/ [GET]上给出内部服务器错误(带有异常)并引发TemplateNotFound,但它在本地主机上成功运行。在运行heroku日志--tails后,我得到了以下问题:

2019-07-09T10:28:00.953042+00:00 app[web.1]: [2019-07-09 10:28:00,950] ERROR in app: Exception on / [GET]
2019-07-09T10:28:00.953061+00:00 app[web.1]: Traceback (most recent call last):
2019-07-09T10:28:00.953064+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
2019-07-09T10:28:00.953066+00:00 app[web.1]: response = self.full_dispatch_request()
2019-07-09T10:28:00.953069+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
2019-07-09T10:28:00.953071+00:00 app[web.1]: rv = self.handle_user_exception(e)
2019-07-09T10:28:00.953074+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
2019-07-09T10:28:00.953076+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2019-07-09T10:28:00.953078+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2019-07-09T10:28:00.953081+00:00 app[web.1]: raise value
2019-07-09T10:28:00.953083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
2019-07-09T10:28:00.953085+00:00 app[web.1]: rv = self.dispatch_request()
2019-07-09T10:28:00.953087+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
2019-07-09T10:28:00.953089+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2019-07-09T10:28:00.953091+00:00 app[web.1]: File "/app/Credit.py", line 52, in home
2019-07-09T10:28:00.953094+00:00 app[web.1]: return render_template('home.html')
2019-07-09T10:28:00.953095+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/templating.py", line 138, in render_template
2019-07-09T10:28:00.953098+00:00 app[web.1]: ctx.app.jinja_env.get_or_select_template(template_name_or_list),
2019-07-09T10:28:00.953104+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/jinja2/environment.py", line 869, in get_or_select_template
2019-07-09T10:28:00.953106+00:00 app[web.1]: return self.get_template(template_name_or_list, parent, globals)
2019-07-09T10:28:00.953107+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
2019-07-09T10:28:00.953109+00:00 app[web.1]: return self._load_template(name, self.make_globals(globals))
2019-07-09T10:28:00.953110+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
2019-07-09T10:28:00.953112+00:00 app[web.1]: template = self.loader.load(self, name, globals)
2019-07-09T10:28:00.953113+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/jinja2/loaders.py", line 113, in load
2019-07-09T10:28:00.953115+00:00 app[web.1]: source, filename, uptodate = self.get_source(environment, name)
2019-07-09T10:28:00.953117+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
2019-07-09T10:28:00.953118+00:00 app[web.1]: return self._get_source_fast(environment, template)
2019-07-09T10:28:00.953120+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
2019-07-09T10:28:00.953121+00:00 app[web.1]: raise TemplateNotFound(template)
2019-07-09T10:28:00.953127+00:00 app[web.1]: jinja2.exceptions.TemplateNotFound: home.html

我的代码在Credit.py文件中

以下是我的Procfile:

web: gunicorn Credit:app

以下是我的要求。txt:

Click==7.0
Flask==1.1.1
Flask-SQLAlchemy==2.4.0
Flask-WTF==0.14.2
gunicorn==19.9.0
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
SQLAlchemy==1.3.5
Werkzeug==0.15.4
WTForms==2.2.1

目录结构:

|   .gitignore
|   Credit.py
|   Procfile
|   requirements.txt
|   site.db
|   tree.txt
|   
+---env
+---templates
|   |   about.html
|   |   accounts.html
|   |   home.html
|   |   layout.html
|   |   transactions.html
|   |   users.html
|   |   
|   \---includes
|           _navbar
|           _navbar.html
|           
\---__pycache__
        Credit.cpython-37.pyc

未添加env目录结构,因为它太大了

我的credit.py文件太大,无法共享,因此可以在https://github.com/Shrikant991/Credit-Management-System-in-Flask/blob/master/Credit.py

上查看

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

根据您的git repo和目录结构,很可能是由于文件夹templates t区分大小写而引起的。

尝试为此编辑代码。 app = Flask(__name__, template_folder='Templates') 或将您的Templates文件夹重命名为templates

答案 1 :(得分:0)

“模板”的字母“ t”必须大写。 'DIRS':[os.path.join(BASE_DIR,'Templates')],