我有这样的代码,有嵌套的字典,我需要把它放在一个Jinja2模板中,但没有任何运气。
from jinja2 import Template
vlans = {"1": {"description": "default", "name": "default", "IP": {"IP1": "10.0.0.1", "IP2": "20.0.0.2"}}}
vlan_template = Template('''
{% for key,value in vlans.items() %}
vlan {{ key }}
description {{ value.description }}
name {{ value.name }}
{% if value == "IP"%}
{% for subkey,subvalue in value["IP"].items() %}
ip address {{ subvalue.IP1 }}
ip address {{ subvalue.IP2 }} secondary
{% endfor %}
{% endif %}
{% endfor %}
print(vlan_template.render(vlans = vlans))
我试图得到以下输出结果
vlan 1
description default
name default
ip address 10.0.0.1
ip address 20.0.0.2 secondary