如何修改/合并 Jinja2 词典? [英] How can I modify/merge Jinja2 dictionaries?
问题描述
我有一个 Jinja2 字典,我想要一个修改它的表达式 - 要么改变它的内容,要么与另一个字典合并.
<预><代码>>>>进口jinja2>>>e = jinja2.Environment()修改字典:失败.
<预><代码>>>>e.from_string("{{ x[4]=5 }}").render({'x':{1:2,2:3}})回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件jinja2/environment.py",第 743 行,在 from_string 中返回 cls.from_code(self, self.compile(source), globals, None)编译中的文件jinja2/environment.py",第 469 行self.handle_exception(exc_info, source_hint=source)文件<unknown>",第 1 行,在模板中jinja2.exceptions.TemplateSyntaxError:预期令牌'打印语句结束',得到'='两阶段更新:打印多余的无".
<预><代码>>>>e.from_string("{{ x.update({4:5}) }} {{ x }}").render({'x':{1:2,2:3}})u'无 {1: 2, 2: 3, 4: 5}'>>>e.from_string("{{ dict(x.items()+ {3:4}.items()) }}").render({'x':{1:2,2:3}})回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件jinja2/environment.py",第 868 行,在渲染中返回 self.environment.handle_exception(exc_info, True)顶级模板代码中的文件",第 1 行类型错误:<lambda>() 需要 0 个参数(给定 1 个)使用dict(x,**y)
:失败.
那么在 Jinja2 中如何通过改变一个属性或与另一个字典合并来修改字典 x
?
这个问题类似于:How我可以将两个 Python 字典合并为一个表达式吗? -- 只要 Jinja2 和 Python 是类似的.
我找到了另一个没有任何扩展的解决方案.
{% set _dummy = x.update({4:5}) %}
它使 x 更新.不要使用_dummy.
I have a Jinja2 dictionary and I want a single expression that modifies it - either by changing its content, or merging with another dictionary.
>>> import jinja2
>>> e = jinja2.Environment()
Modify a dict: Fails.
>>> e.from_string("{{ x[4]=5 }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jinja2/environment.py", line 743, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "jinja2/environment.py", line 469, in compile
self.handle_exception(exc_info, source_hint=source)
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token
'end of print statement', got '='
Two-stage update: Prints superfluous "None".
>>> e.from_string("{{ x.update({4:5}) }} {{ x }}").render({'x':{1:2,2:3}})
u'None {1: 2, 2: 3, 4: 5}'
>>> e.from_string("{{ dict(x.items()+ {3:4}.items()) }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jinja2/environment.py", line 868, in render
return self.environment.handle_exception(exc_info, True)
File "<template>", line 1, in top-level template code
TypeError: <lambda>() takes exactly 0 arguments (1 given)
Use dict(x,**y)
: Fails.
>>> e.from_string("{{ dict((3,4), **x) }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jinja2/environment.py", line 868, in render
return self.environment.handle_exception(exc_info, True)
File "<template>", line 1, in top-level template code
TypeError: call() keywords must be strings
So how does one modify the dictionary x
in Jinja2 by changing an attribute or merging with another dictionary?
This question is similar to: How can I merge two Python dictionaries as a single expression? -- insofar as Jinja2 and Python are analogous.
I found another solution without any extension.
{% set _dummy = x.update({4:5}) %}
It makes x updated. Don't use _dummy.
这篇关于如何修改/合并 Jinja2 词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!