Python+Flask - 在 render.com 上部署时获取“jinja2.exceptions.TemplateNotFound: index.html”(本地工作正常)

Python+Flask - Getting "jinja2.exceptions.TemplateNotFound: index.html" when deploying on render.com (works fine locally)

提问人:pchryss 提问时间:8/16/2023 更新时间:8/18/2023 访问量:56

问:

这是我第一次尝试部署 Web 服务

在过去的 2 个月里,我一直在本地开发一个烧瓶应用程序,并且在查找模板时从未遇到过任何问题。当我在 render.com 上部署后尝试访问我的 Web 应用时,它会在网站上显示此消息

内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中出现错误

这些是发生错误后显示的日志

\

Aug 15 03:53:33 PM  [2023-08-15 19:53:33,394] ERROR in app: Exception on / [GET]
Aug 15 03:53:33 PM  Traceback (most recent call last):
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2529, in wsgi_app
Aug 15 03:53:33 PM      response = self.full_dispatch_request()
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1825, in full_dispatch_request
Aug 15 03:53:33 PM      rv = self.handle_user_exception(e)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1823, in full_dispatch_request
Aug 15 03:53:33 PM      rv = self.dispatch_request()
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
Aug 15 03:53:33 PM      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
Aug 15 03:53:33 PM    File "/opt/render/project/src/app/views.py", line 20, in home
Aug 15 03:53:33 PM      return render_template('index.html')
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 146, in render_template
Aug 15 03:53:33 PM      template = app.jinja_env.get_or_select_template(template_name_or_list)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1081, in get_or_select_template
Aug 15 03:53:33 PM      return self.get_template(template_name_or_list, parent, globals)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1010, in get_template
Aug 15 03:53:33 PM      return self._load_template(name, globals)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 969, in _load_template
Aug 15 03:53:33 PM      template = self.loader.load(self, name, self.make_globals(globals))
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/loaders.py", line 126, in load
Aug 15 03:53:33 PM      source, filename, uptodate = self.get_source(environment, name)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 62, in get_source
Aug 15 03:53:33 PM      return self._get_source_fast(environment, template)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 98, in _get_source_fast
Aug 15 03:53:33 PM      raise TemplateNotFound(template)
Aug 15 03:53:33 PM  jinja2.exceptions.TemplateNotFound: index.html
Aug 15 03:53:33 PM  127.0.0.1 - - [15/Aug/2023:19:53:33 +0000] "GET / HTTP/1.1" 500 265 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
Aug 15 03:53:33 PM  [2023-08-15 19:53:33,576] ERROR in app: Exception on / [GET]
Aug 15 03:53:33 PM  Traceback (most recent call last):
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2529, in wsgi_app
Aug 15 03:53:33 PM      response = self.full_dispatch_request()
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1825, in full_dispatch_request
Aug 15 03:53:33 PM      rv = self.handle_user_exception(e)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1823, in full_dispatch_request
Aug 15 03:53:33 PM      rv = self.dispatch_request()
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
Aug 15 03:53:33 PM      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
Aug 15 03:53:33 PM    File "/opt/render/project/src/app/views.py", line 20, in home
Aug 15 03:53:33 PM      return render_template('index.html')
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 146, in render_template
Aug 15 03:53:33 PM      template = app.jinja_env.get_or_select_template(template_name_or_list)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1081, in get_or_select_template
Aug 15 03:53:33 PM      return self.get_template(template_name_or_list, parent, globals)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1010, in get_template
Aug 15 03:53:33 PM      return self._load_template(name, globals)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 969, in _load_template
Aug 15 03:53:33 PM      template = self.loader.load(self, name, self.make_globals(globals))
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/loaders.py", line 126, in load
Aug 15 03:53:33 PM      source, filename, uptodate = self.get_source(environment, name)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 62, in get_source
Aug 15 03:53:33 PM      return self._get_source_fast(environment, template)
Aug 15 03:53:33 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 98, in _get_source_fast
Aug 15 03:53:33 PM      raise TemplateNotFound(template)
Aug 15 03:53:33 PM  jinja2.exceptions.TemplateNotFound: index.html
Aug 15 03:53:33 PM  127.0.0.1 - - [15/Aug/2023:19:53:33 +0000] "GET / HTTP/1.1" 500 265 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
Aug 15 03:53:39 PM  [2023-08-15 19:53:39,371] ERROR in app: Exception on / [GET]
Aug 15 03:53:39 PM  Traceback (most recent call last):
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2529, in wsgi_app
Aug 15 03:53:39 PM      response = self.full_dispatch_request()
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1825, in full_dispatch_request
Aug 15 03:53:39 PM      rv = self.handle_user_exception(e)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1823, in full_dispatch_request
Aug 15 03:53:39 PM      rv = self.dispatch_request()
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
Aug 15 03:53:39 PM      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
Aug 15 03:53:39 PM    File "/opt/render/project/src/app/views.py", line 20, in home
Aug 15 03:53:39 PM      return render_template('index.html')
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 146, in render_template
Aug 15 03:53:39 PM      template = app.jinja_env.get_or_select_template(template_name_or_list)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1081, in get_or_select_template
Aug 15 03:53:39 PM      return self.get_template(template_name_or_list, parent, globals)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 1010, in get_template
Aug 15 03:53:39 PM      return self._load_template(name, globals)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/environment.py", line 969, in _load_template
Aug 15 03:53:39 PM      template = self.loader.load(self, name, self.make_globals(globals))
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/jinja2/loaders.py", line 126, in load
Aug 15 03:53:39 PM      source, filename, uptodate = self.get_source(environment, name)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 62, in get_source
Aug 15 03:53:39 PM      return self._get_source_fast(environment, template)
Aug 15 03:53:39 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 98, in _get_source_fast
Aug 15 03:53:39 PM      raise TemplateNotFound(template)
Aug 15 03:53:39 PM  jinja2.exceptions.TemplateNotFound: index.html
Aug 15 03:53:39 PM  127.0.0.1 - - [15/Aug/2023:19:53:39 +0000] "GET / HTTP/1.1" 500 265 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"

This is the file structure of my project

初始化 .py

load_dotenv()
app = Flask('__main__', static_folder='app/static', template_folder='app/templates')

from app import views

views.py

@app.route('/', methods=['POST', 'GET'] )
def home():
    return render_template('index.html')

run.py

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)
Python 模板 Flask 部署 render.com

评论

0赞 pchryss 8/16/2023
仅供参考:图像是我的文件结构。它在我的本地计算机上工作正常,但在部署时无法正常工作。
0赞 Diego Borba 8/16/2023
请给出服务器文件的结构
0赞 pchryss 8/16/2023
@DiegoBorba 在哪里可以找到这些?在 render.com 上还是在我的项目代码中?对不起,我是虚拟主机新手
0赞 Diego Borba 8/16/2023
你的应用在 git 存储库中,对吧?
0赞 pchryss 8/16/2023
@DiegoBorba 是的!

答:

0赞 Stefan Glova 8/16/2023 #1

我认为您需要为您的项目字典使用绝对路径:

import os

static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app', 'static')
template_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app', 'templates')

app = Flask('__main__', static_folder=static_folder, template_folder=template_folder)

评论

1赞 pchryss 8/16/2023
刚刚试过这个。收到相同的错误
1赞 pchryss 8/18/2023 #2

最终的问题是我试图使用相对路径查找我的模板文件夹。我需要使用绝对路径才能找到我的模板文件

template_folder='app/templates'

template_folder='/pathtoapp/app/templates'