【问题标题】:Flask cannot find HTML files in templates folder when run through Gunicorn通过 Gunicorn 运行时,Flask 在模板文件夹中找不到 HTML 文件
【发布时间】:2019-05-22 03:34:49
【问题描述】:

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

我用 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)

任何帮助表示赞赏。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请让您的问题自成一体。不要发布错误消息的链接,将它们作为代码格式的文本放在您的问题中。
  • @roganjosh 谢谢,整理好了。
  • 这个链接可以帮到你:stackoverflow.com/questions/21765692/…

标签: python python-2.7 flask gunicorn


【解决方案1】:

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

application = Flask(__name__, template_folder='../../')

当脚本是 Python 解释器的入口点时,Python 将 __name__ 设置为 '__main__'。否则它将评估为模块的名称。另外请注意,如果您未在 gunicorn 命令中指定应用名称,则必须使用 application 而不是 app

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

【讨论】:

    猜你喜欢
    • 2017-08-13
    • 2014-02-17
    • 2019-01-02
    • 1970-01-01
    • 2021-10-15
    • 2011-02-17
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多