一尘不染

解决Gunicorn下Flask的TemplateNotFound错误

flask

我有一个Flask应用程序,正在尝试使用Gunicorn和nginx进行部署。但是,尽管它在本地运行良好,但是当我在远程服务器上使用Gunicorn运行时,它会引发TemplateNotFound错误。

我不确定如何开始调试它,更不用说它为什么会失败了……希望对前者有所帮助,即使不是后者。我以为这可能是权限问题,所以将模板文件夹更改为777 …没有运气。这是所有相关的细节:

安装脚本
从纯粹的Ubuntu 10.04安装开始,我运行它来设置服务器并插入我的代码:https : //github.com/total-impact/total-impact-deploy/blob/master/deploy.sh。然后,我将此nginx配置文件放在/ etc / nginx / sites-available / total-impact中:

server {
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

最后,我浏览应用程序目录并运行gunicorn web:app,然后点击服务器的IP地址。这将在浏览器中生成500,并在命令行上输出:

堆栈跟踪:

root@jc:/home/ti/total-impact-webapp/totalimpactwebapp# gunicorn web:app2012-05-28 23:15:06 [15313] [INFO] Starting gunicorn 0.14.3
2012-05-28 23:15:06 [15313] [INFO] Listening at: http://127.0.0.1:8000 (15313)
2012-05-28 23:15:06 [15313] [INFO] Using worker: sync
2012-05-28 23:15:06 [15316] [INFO] Booting worker with pid: 15316
2012-05-28 23:15:12,274 - totalimpactwebapp.core - ERROR - Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1062, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1060, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1047, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ti/total-impact-webapp/totalimpactwebapp/web.py", line 60, in home
    return render_template('index.html', commits=False)
  File "/usr/local/lib/python2.6/dist-packages/flask/templating.py", line 120, in render_template
    return _render(ctx.app.jinja_env.get_template(template_name),
  File "/usr/local/lib/python2.6/dist-packages/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python2.6/dist-packages/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python2.6/dist-packages/jinja2/loaders.py", line 115, in load source, filename, uptodate = self.get_source(environment, name)
  File "/usr/local/lib/python2.6/dist-packages/flask/templating.py", line 61, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: index.html

阅读 652

收藏
2020-04-08

共1个答案

一尘不染

你的模板位于[app root] / templates /中吗?

如果是这样,请检查以确保你的路径正确。将其作为处理首页的视图的第一行:

return app.root_path

如果这是你希望看到的-或者你正在使用蓝图或以某种方式更改默认Jinja Environment的其他方法-则要复杂一些。

奇怪的是,Jinja似乎没有jinja2.Environment.FileSystemLoader.get_search_path()方法。我以为它会有一个:(

2020-04-08