【发布时间】:2021-01-16 20:15:33
【问题描述】:
jinja_code.py
import jinja2
env=jinja2.Environment(loader=FileSystemLoader(searchpath="./"))
template=env.get_template('file.j2')
render_template=template.render(test1="TEST1",test2="TEST2")
print(render_template)
文件.j2
{{ context.test1 }}
我正在学习 Jinja2,我知道上下文是传递给 Template 的变量,但是当我执行上面的代码时,我得到了以下错误
jinja2.exceptions.undefinederror: 'context' is not defined
我已阅读文档,但无法完全理解。您能否解释一下什么是上下文以及如何使用它来访问变量?
【问题讨论】:
-
在没有上下文的情况下尝试
{{test1}} -
是的,我知道这行得通。但是我看到了一个代码,用户只是写了 {% set name = context.FirstName %}。所以我很困惑这个上下文是从哪里加载的?
标签: python templates django-templates jinja2