无法使用 jinja2 加载模板
Can't load templates with jinja2
我有以下项目,
APP
|-static
|-templates
|-file.html
|-blueprints
|-blueprint1.py
|-blueprint2.py
|-app.py
每个蓝图文件都有各种 sanic
路由,我想在调用时呈现模板。
我尝试将以下内容放入每个 blueprint
文件中,
template_env = Environment(
loader=PackageLoader('APP', 'static/templates'),
autoescape=select_autoescape(['html', 'xml'])
)
只得到错误ModuleNotFoundError: No module named 'APP'
用 blueprints
替换 APP
给我错误 TypeError: expected str, bytes or os.PathLike object, not NoneType
我也试过用这样的FileSystemLoader
,
template_loader = FileSystemLoader(searchpath="../static/templates")
template_env = Environment(loader=template_loader)
并加载我需要的模板template = template_env.get_template('file.html')
但是我在访问 url 时得到 template not found
。
直接尝试渲染我的模板,
with open('..static/templates/file.html') as file:
template = Template(file.read())
再次导致 file not found
错误。
在我的应用中使用 jinja
模板的最佳方式是什么?
我在 中创建了一个项目,我向 jinja 模板渲染了一个值,它工作正常你可以看看这个我希望会有所帮助:
这是项目的树:
.
├── app.py
└── static
└── templates
└── template.html
2 directories, 2 files
这里是 template.html:
<html>
<header><title>This is title</title></header>
<body>
<p>{{ value }}!</p>
</body>
</html>
这是 app.py :
#!/usr/bin/python
import jinja2
import os
path=os.path.join(os.path.dirname(__file__),'./static/templates')
templateLoader = jinja2.FileSystemLoader(searchpath=path)
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.html"
hello="hello..... "
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render(value=hello) # this is where to put args to the template renderer
print(outputText)
输出:
<html>
<header><title>This is title</title></header>
<body>
</body>
</html>
@gh-laptop:~/jinja$ python app.py
<html>
<header><title>This is title</title></header>
<body>
<p>hello..... !</p>
</body>
</html>
简单解释 PackageLoader
的工作原理:定义的模板文件夹(第二个参数:package_path)应该相对于包含从 python 中可见的模块的文件夹(第一个参数: package_name).
因此 APP
不是模块,您应该使用 app
代替。由于模板文件夹将相对于 APP
(包含 app
的文件夹),因此 package_path
是好的。
我有以下项目,
APP
|-static
|-templates
|-file.html
|-blueprints
|-blueprint1.py
|-blueprint2.py
|-app.py
每个蓝图文件都有各种 sanic
路由,我想在调用时呈现模板。
我尝试将以下内容放入每个 blueprint
文件中,
template_env = Environment(
loader=PackageLoader('APP', 'static/templates'),
autoescape=select_autoescape(['html', 'xml'])
)
只得到错误ModuleNotFoundError: No module named 'APP'
用 blueprints
替换 APP
给我错误 TypeError: expected str, bytes or os.PathLike object, not NoneType
我也试过用这样的FileSystemLoader
,
template_loader = FileSystemLoader(searchpath="../static/templates")
template_env = Environment(loader=template_loader)
并加载我需要的模板template = template_env.get_template('file.html')
但是我在访问 url 时得到 template not found
。
直接尝试渲染我的模板,
with open('..static/templates/file.html') as file:
template = Template(file.read())
再次导致 file not found
错误。
在我的应用中使用 jinja
模板的最佳方式是什么?
我在 中创建了一个项目,我向 jinja 模板渲染了一个值,它工作正常你可以看看这个我希望会有所帮助: 这是项目的树:
.
├── app.py
└── static
└── templates
└── template.html
2 directories, 2 files
这里是 template.html:
<html>
<header><title>This is title</title></header>
<body>
<p>{{ value }}!</p>
</body>
</html>
这是 app.py :
#!/usr/bin/python
import jinja2
import os
path=os.path.join(os.path.dirname(__file__),'./static/templates')
templateLoader = jinja2.FileSystemLoader(searchpath=path)
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.html"
hello="hello..... "
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render(value=hello) # this is where to put args to the template renderer
print(outputText)
输出:
<html>
<header><title>This is title</title></header>
<body>
</body>
</html>
@gh-laptop:~/jinja$ python app.py
<html>
<header><title>This is title</title></header>
<body>
<p>hello..... !</p>
</body>
</html>
简单解释 PackageLoader
的工作原理:定义的模板文件夹(第二个参数:package_path)应该相对于包含从 python 中可见的模块的文件夹(第一个参数: package_name).
因此 APP
不是模块,您应该使用 app
代替。由于模板文件夹将相对于 APP
(包含 app
的文件夹),因此 package_path
是好的。