TemplateNotFound 即使我可以打印出目录
TemplateNotFound even though I can print out the directory
我正在将 swaggerUI 添加到 Flask API。我有我的应用程序,在应用程序内部我创建了一个模板所在的 swagger 目录。当在 __init__
文件中初始化 flask 应用程序时,我设置了 template_folder。这似乎有效,因为它在函数内打印出正确的目录。我什至做了一个 ls 并且 swaggerui.html 文件在那里,但我仍然收到错误消息,提示找不到模板。另外,如果我在 templates_folder 中添加一个随机文件 testin123.txt,当我刷新站点时,会打印出 testing123.txt。我错过了什么?
from flask import Flask, request, jsonify, render_template, Blueprint
from flask import current_app as current_app
import os
docs = Blueprint('docs', __name__)
@docs.route('/docs')
def get_docs():
#this prints out the correct path
print(f"template dir is {current_app.template_folder}")
#this prints "swaggerui.html"
for filename in os.listdir(current_app.template_folder):
print(filename)
print("After ls")
#even though everything check-out still cant find file
return render_template('swaggerui.html')
template dir is flask_project/swagger/templates/
swaggerui.html
test123.txt
After ls
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/app/./flask_project/docs.py", line 28, in get_docs
return render_template('swaggerui.html')
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 997, in get_template
return self._load_template(name, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 59, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 95, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: swaggerui.html
.
├── __init__.py
├── docs.py
├── swagger
│ ├── static
│ │ ├── css
│ │ │ └── swagger-ui.css
│ │ ├── img
│ │ │ ├── favicon-16x16.png
│ │ │ └── favicon-32x32.png
│ │ ├── js
│ │ │ ├── swagger-ui-bundle.js
│ │ │ ├── swagger-ui-standalone-preset.js
│ │ │ └── swagger-ui.js
│ │ └── openapi.json
│ └── templates
│ └── swaggerui.html
在您的应用中设置 EXPLAIN_TEMPLATE_LOADING 选项并在此处传递输出。
myApp = Flask(__name__)
myApp.config["EXPLAIN_TEMPLATE_LOADING"] = True
也检查this
我正在将 swaggerUI 添加到 Flask API。我有我的应用程序,在应用程序内部我创建了一个模板所在的 swagger 目录。当在 __init__
文件中初始化 flask 应用程序时,我设置了 template_folder。这似乎有效,因为它在函数内打印出正确的目录。我什至做了一个 ls 并且 swaggerui.html 文件在那里,但我仍然收到错误消息,提示找不到模板。另外,如果我在 templates_folder 中添加一个随机文件 testin123.txt,当我刷新站点时,会打印出 testing123.txt。我错过了什么?
from flask import Flask, request, jsonify, render_template, Blueprint
from flask import current_app as current_app
import os
docs = Blueprint('docs', __name__)
@docs.route('/docs')
def get_docs():
#this prints out the correct path
print(f"template dir is {current_app.template_folder}")
#this prints "swaggerui.html"
for filename in os.listdir(current_app.template_folder):
print(filename)
print("After ls")
#even though everything check-out still cant find file
return render_template('swaggerui.html')
template dir is flask_project/swagger/templates/
swaggerui.html
test123.txt
After ls
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/app/./flask_project/docs.py", line 28, in get_docs
return render_template('swaggerui.html')
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 997, in get_template
return self._load_template(name, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 59, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 95, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: swaggerui.html
.
├── __init__.py
├── docs.py
├── swagger
│ ├── static
│ │ ├── css
│ │ │ └── swagger-ui.css
│ │ ├── img
│ │ │ ├── favicon-16x16.png
│ │ │ └── favicon-32x32.png
│ │ ├── js
│ │ │ ├── swagger-ui-bundle.js
│ │ │ ├── swagger-ui-standalone-preset.js
│ │ │ └── swagger-ui.js
│ │ └── openapi.json
│ └── templates
│ └── swaggerui.html
在您的应用中设置 EXPLAIN_TEMPLATE_LOADING 选项并在此处传递输出。
myApp = Flask(__name__)
myApp.config["EXPLAIN_TEMPLATE_LOADING"] = True
也检查this