我有一个Jinja2字典,我想要一个修改它的表达式 - 通过改变它的内容,或者与另一个字典合并.
>>> import jinja2
>>> e = jinja2.Environment()
Run Code Online (Sandbox Code Playgroud)
修改字典:失败.
>>> 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 '='
Run Code Online (Sandbox Code Playgroud)
两阶段更新:打印多余的"无".
>>> 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}}) …
Run Code Online (Sandbox Code Playgroud)