首页 > 解决方案 > 无法在生产服务器上使用烧瓶渲染模板

问题描述

在我的家庭开发服务器上,按照代码运行时,一切正常

from flask import Flask, render_template

app = Flask("__name__")


@app.route("/")
def home():
    return "<h1>Hello world</h1>"


@app.route("/test")
def test():
    return render_template("index.html")


if __name__ == "__main__":
    app.run()

转到http://127.0.0.1:5000正确显示“Hello World”,/test 正确显示我的 html 模板。

但是,当我尝试在生产服务器上运行具有相同文件结构的相同代码时,我能够使用内联 html 显示文本('serverIP'/ 仍然显示“Hello World”),但每当我尝试使用 render_template功能,它不起作用('serverIP'/test 给出内部服务器错误)

我确定我的 html 模板位于正确的目录中

我对这一切都很陌生,所以任何关于问题可能出在哪里的见解都将不胜感激。如果有帮助,运行服务器的机器使用 Ubuntu 18.04 和 Apache

抛出的错误是:jinja2.exceptions.TemplateNotFound: index.html

.
|-- webApp
|   |-- __init__.py
|   |-- static
|   `-- templates
|       |-- index.html
`-- webapp.wsgi

错误日志:

[Tue Jun 15 21:26:15.165189 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391] ERROR:__name__:Exception on /test [GET]
[Tue Jun 15 21:26:15.165235 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391] Traceback (most recent call last):
[Tue Jun 15 21:26:15.165239 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2070, in wsgi_app
[Tue Jun 15 21:26:15.165243 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     response = self.full_dispatch_request()
[Tue Jun 15 21:26:15.165246 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1515, in full_dispatch_request
[Tue Jun 15 21:26:15.165250 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     rv = self.handle_user_exception(e)
[Tue Jun 15 21:26:15.165253 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1513, in full_dispatch_request
[Tue Jun 15 21:26:15.165256 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     rv = self.dispatch_request()
[Tue Jun 15 21:26:15.165259 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1499, in dispatch_request
[Tue Jun 15 21:26:15.165262 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
[Tue Jun 15 21:26:15.165266 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/var/www/webApp/webApp/__init__.py", line 13, in test
[Tue Jun 15 21:26:15.165269 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     return render_template("index.html")
[Tue Jun 15 21:26:15.165272 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/templating.py", line 148, in render_template
[Tue Jun 15 21:26:15.165287 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     ctx.app.jinja_env.get_or_select_template(template_name_or_list),
[Tue Jun 15 21:26:15.165290 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/jinja2/environment.py", line 1068, in get_or_select_template
[Tue Jun 15 21:26:15.165293 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     return self.get_template(template_name_or_list, parent, globals)
[Tue Jun 15 21:26:15.165296 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/jinja2/environment.py", line 997, in get_template
[Tue Jun 15 21:26:15.165299 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     return self._load_template(name, globals)
[Tue Jun 15 21:26:15.165302 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/jinja2/environment.py", line 958, in _load_template
[Tue Jun 15 21:26:15.165305 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     template = self.loader.load(self, name, self.make_globals(globals))
[Tue Jun 15 21:26:15.165307 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/jinja2/loaders.py", line 125, in load
[Tue Jun 15 21:26:15.165310 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     source, filename, uptodate = self.get_source(environment, name)
[Tue Jun 15 21:26:15.165313 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/templating.py", line 59, in get_source
[Tue Jun 15 21:26:15.165316 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     return self._get_source_fast(environment, template)
[Tue Jun 15 21:26:15.165319 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]   File "/usr/local/lib/python3.9/dist-packages/flask/templating.py", line 95, in _get_source_fast
[Tue Jun 15 21:26:15.165322 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391]     raise TemplateNotFound(template)
[Tue Jun 15 21:26:15.165324 2021] [wsgi:error] [pid 9931:tid 139653360965184] [client 162.198.172.56:65391] jinja2.exceptions.TemplateNotFound: index.html

标签: pythonlinuxflask

解决方案


是的,我在创建 Flask 实例时犯了一个愚蠢的错误

app = Flask("__name__") 应该 app = Flask(__name__)

谢谢你的帮助


推荐阅读