[英]Flask/Jinja2 - Iterating over nested dictionaries
我試圖以一堆嵌套的無序列表的形式顯示字典的內容和結構。
我設法整理的數據看起來像這樣,
{'.': {'walk.py': None, 'what.html': None, 'misc': {}, 'orders': {'order1.html': None, 'more': {'stuff.html': None}}}}
代表這個目錄樹,
.:
misc/ orders/ walk.py what.html
./misc:
./orders:
more/ order1.html
./orders/more:
stuff.html
我將如何使用Jinja2語法對此進行迭代? 是否有更好的方法可以做到這一點?
非常感謝。
編輯:我感到愚蠢。 再次尋找解決方案后,我確切地找到了我想要的東西。 猜猜我的google-fu並不是第一次嘗試。 這里是...
通過使用for
循環的recursive
修飾符(取自文檔的示例):
<ul class="sitemap">
{%- for item in sitemap recursive %}
<li><a href="{{ item.href|e }}">{{ item.title }}</a>
{%- if item.children -%}
<ul class="submenu">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
更新
這是我想到的:
from jinja2 import Template
x = Template("""{%- for key, value in tree.iteritems() recursive %}
{{ '--' * (loop.depth-1) }}{{ key }}
{%- if value is mapping -%}/{{ loop(value.iteritems()) }}{%- endif -%}
{%- endfor %}
""")
tree = {'.': {
'walk.py': None,
'what.html': None,
'misc': {},
'orders': {
'order1.html': None,
'more': {
'stuff.html': None
}
}
}}
print x.render(tree=tree)
輸出:
./
--walk.py
--what.html
--misc/
--orders/
----order1.html
----more/
------stuff.html
(Jinja2代碼中的破折號(例如{%- ... -%}
-...- {%- ... -%}
用於空格控制 。請嘗試一下。)
為什么不只是嵌套的for循環?
在您的views.py中:
def index(request):
context={'main1':{'sub1','sub2','sub3'},'main2':{'sub1','sub2'}}
return render(request,'index.html',context)
在您的index.html中:
{% for key1,val in context.items %}
<p> {{ key1 }} </p>
<ul>
{% for key2 in val.items %}
<li> {{key2}} </li>
{% endfor %}
</ul>
{% endfor %}
首先整理數據:
a = {'.': {'walk.py': None, 'what.html': None, 'misc': {}, 'orders': {'order1.html': None, 'more': {'stuff.html': None}}}}
from collections import defaultdict
def f(data, path):
for k,v in data.iteritems():
if v is None:
yield path,k
else:
yield path,k+"/"
for k in f(v,path+k+"/"):
yield k
def process_data():
collect = defaultdict(list)
for p in f(a,""):
if p[0]:
collect[p[0][:-1]].append(p[1])
return collect
現在,如果您運行:
data = process_data()
for k in data.keys():
print k,data[k]
你得到:
./orders ['order1.html', 'more/']
./orders/more ['stuff.html']
. ['walk.py', 'what.html', 'misc/', 'orders/']
多數民眾贊成在您需要渲染。 模板應類似於:
{% for k in sitemap.keys()|sort -%}
{{ k }}:<br/>
{% for v in sitemap[k] %}
{{ v }}
{%- endfor %}
<br/>
{%- endfor %}
以及渲染請求:
@app.route("/")
def hello():
return render_template('temp.html',sitemap=process_data())
在我的測試中呈現為:
.:<br/>
walk.py
what.html
misc/
orders/
<br/>./orders:<br/>
order1.html
more/
<br/>./orders/more:<br/>
stuff.html
<br/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.