Reputation: 1668
If an object obj
has no attribute foo
, then I would like referring to {{obj.foo}}
in a Jinja2 template to fail when rendering.
Currently I'm getting the template text with empty variables. How to get the standard AttributeError
exception instead?
Sample code looks like this:
class Foo:
pass
env = Environment(
loader=PackageLoader("mydistro"),
autoescape=select_autoescape()
)
t = env.get_template("template/path/to.j2")
print(t.render(obj=Foo()))
Upvotes: 0
Views: 55
Reputation: 169338
Initialize your environment with StrictUndefined
as the undefined
class.
env = Environment(
loader=PackageLoader("mydistro"),
autoescape=select_autoescape(),
undefined=StrictUndefined,
)
Upvotes: 2