0
当有一本字典:嵌套的字典和Django的模板
menu = {'title': 'Link1',
'children': {'title': 'Child of Link1',
'children': #etc..}
}
如何将一个叠代,使输出变为:
<ul>
<li>Link1
<ul>
<li>Child of Link 1
<ul>
<li>Grandchild of Link 1
<ul>etc..</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
目前我使用这一点,但它显然只是去到一个深度。
<ul id="mainnavigation">
{% for k,v in admin.items %} #ContextProcessor, because this menu needs to know the current path.
<li class="expandable"><a href="{{ v.path }}">{{ v.title }}</a>
{% if v.children != None %}
<ul>
{% for id,child in v.children.items %}
<li class="expandable"><a href="{{ child.path }}">{{ child.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
一种方法是手动重复每个级别的循环,但这样的解决方案是丑陋的,我希望有一个更干的。