Web ページに辞書の内容を表示したい。辞書構造は再帰的です。私はそれを機能させることはできません。代わりに、再帰を「手動で展開」する必要があります。おそらく、私はドキュメントを正しく理解していません。
再帰を使用しないコード (そして機能します)。
{% 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/ (再帰を探してください)