当通过 Gunicorn 运行 时,Flask 无法在模板文件夹中找到 HTML 文件

Flask cannot find HTML files in templates folder when run through Gunicorn

当我 运行 我的 Flask 应用程序直接使用 Python(开发模式?)时一切正常,所以模板文件夹位于正确的位置。当我通过 Gunicorn 运行 它时,它一直找不到我的模板文件夹,因此找不到所需的 HTML 文件。我尝试了很多事情,例如更改 template_folder 变量,以及传递给 render_template() - none 的路径有任何影响。

我用 gunicorn -b 0.0.0.0:80 wsgi 启动 gunicorn(没有 wsgi 仍然无法工作),一切正常。

航站楼:

root@vps621912:/home/websites/test2# source test2/bin/activate
(test2) root@vps621912:/home/websites/test2# gunicorn -b 0.0.0.0:80 server:app
[2018-12-21 18:13:55 +0000] [30377] [INFO] Starting gunicorn 19.9.0
[2018-12-21 18:13:55 +0000] [30377] [INFO] Listening at: http://0.0.0.0:80 (30377)
[2018-12-21 18:13:55 +0000] [30377] [INFO] Using worker: sync
[2018-12-21 18:13:55 +0000] [30381] [INFO] Booting worker with pid: 30381
[2018-12-21 18:14:00,240] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/websites/test2/server.py", line 10, in home
    return render_template('home.html')
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/templating.py", line 58, in get_source
    return self._get_source_fast(environment, template)
  File "/home/websites/test2/test2/local/lib/python2.7/site-packages/flask/templating.py", line 86, in _get_source_fast
    raise TemplateNotFound(template)
TemplateNotFound: home.html

代码:

from flask import Flask, render_template


app = Flask('__main__', template_folder='../../')


@app.route('/')
def home():
#    return "<h1>test2 - server.py - raw return</h1>"
    return render_template('home.html')
#    return app.root_path


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

感谢任何帮助。

Flask的第一个参数用于在文件系统上查找资源。在您的情况下,您传递的是 __main__,如果您的应用程序的入口点是 server.py 或您所称的开发模式,这很好。如果您使用 gunicorn 运行 应用程序,那么您的 server.py 脚本将不再是入口点;这意味着将 __main__ 传递给 Flask 将不起作用。请改用以下内容:

application = Flask(__name__, template_folder='../../')
当脚本是 python 解释器的入口点时,

Python 将 __name__ 设置为 '__main__'。否则它将评估为模块的名称。另外请注意,如果您没有在 gunicorn 命令中指定应用程序名称,则必须使用 application 而不是 app

如果您使用包而不是单个模块来 运行 您的应用程序,这可能不起作用。您可以找到更多信息 here.