我想在网页中显示字典的内容。字典结构是递归的。我不能让它像那样工作。相反,我必须“手动展开”递归。可能,我没有正确理解文档。
不使用递归的代码(并且有效)。
{% extends "base.html" %}
{% block body %}
<form action="">
{% for account in account_list['account_list'] %}
<ul>
<input type="checkbox" id="{{ account['id'] }}" value="{{ account['name'] }}" type="{{ account['type'] }}"> {{ account['name'] }} <br>
{% for key, value in account.items() %}
{% if value is not string %}
{% for acc in value %}
{% if acc is mapping %}
<ul>
<input type="checkbox" id="{{ acc['id'] }}" value="{{ acc['name'] }}" type="{{ acc['type'] }}"> {{ acc['name'] }} <br>
{% for ke, va in acc.items() %}
{% if va is not string %}
{% for ac in va %}
{% if ac is mapping %}
<ul>
<input type="checkbox" id="{{ ac['id'] }}" value="{{ ac['name'] }}" type="{{ ac['type'] }}"> {{ ac['name'] }} <br>
</ul>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</form>
{% endblock %}
带有递归的代码,但它不起作用。
{% extends "base.html" %}
{% block body %}
<form action="">
{% for account in account_list['account_list'] %}
<ul>
<input type="checkbox" id="{{ account['id'] }}" value="{{ account['name'] }}" type="{{ account['type'] }}"> {{ account['name'] }} <br>
{% for key, value in account.items() recursive %}
{% if value is not string %}
{% for acc in value %}
{% if acc is mapping %}
<ul>
<input type="checkbox" id="{{ acc['id'] }}" value="{{ acc['name'] }}" type="{{ acc['type'] }}"> {{ acc['name'] }} <br>
{% loop(acc.items()) %}
</ul>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</form>
{% endblock %}
相关文档: http: //jinja.pocoo.org/docs/templates/(查找递归)