当我直接使用Python(开发模式?)运行Flask应用程序时,一切正常,因此,templates文件夹位于正确的位置。当我通过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)
任何帮助表示赞赏。
答案 0 :(得分:1)
赋予Flask
的第一个参数用于在文件系统上查找资源。在您的情况下,您正在传递__main__
,如果应用程序的入口点为server.py
或称为开发模式,则很好。如果您使用gunicorn运行应用程序,那么您的server.py
脚本将不再是入口点;这意味着无法将__main__
传递到Flask
。请改用以下内容:
application = Flask(__name__, template_folder='../../')
当脚本是python解释器的入口点时,Python会将__name__
设置为'__main__'
。否则,它将评估为模块的名称。另外,请注意,如果未在gunicorn命令中指定应用名称,则必须使用application
而不是app
。
如果您使用软件包而不是单个模块来运行您的应用程序,则此方法可能不起作用。您可以找到更多信息here。