问题描述
当我们传递模板中不存在的变量时,jinja2是否存在引发异常的方法?
Is there a method for jinja2 to raise an exception when we pass a variable that is not present in the template?
PS:这与在模板中存在变量但未传递变量时引发异常不同(或相反).为此,我使用"undefined = StrictUndefined"
PS: This is different(or opposite) from raising an exception when a variable is present in the template but it is not passed. For this I use "undefined=StrictUndefined"
推荐答案
在加载jinja2.Environment时,请将"undefined"参数设置为"jinja2.StrictUndefined",例如:
When you load your jinja2.Environment, set the 'undefined' parameter to 'jinja2.StrictUndefined', e.g.:
env = jinja2.Environment(loader=<someloader>, undefined=jinja2.StrictUndefined)
您可以捕获并检查渲染异常以查看缺少的内容
You can catch and examine the render exception to see what was missing
编辑,如果我阅读了您的完整问题,将会有所帮助. :)
EDIT It would help if I read your full question. :)
这篇关于如果我们传入模板中不存在的变量,则会在jinja中引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!