[英]Django Template - How to Iterate and access a dictionary?
我有一本字典要遍歷,將其打印在我的網頁上並得到:
dict = {
u'256': <itertools._grouper object at 0x8361d0e90>,
u'59': <itertools._grouper object at 0x83607c110>,
u'59': <itertools._grouper object at 0x83607c050>,
u'1975555': <itertools._grouper object at 0x8361bc490>,
u'91': <itertools._grouper object at 0x836162110>
}
如何在字典中訪問此對象的值? 這是我嘗試過的:
{% for code, codelist in dict.items %}
{% for innerlists in codelist %}
// Here I want to display the values in the dict's object
{% endfor %}
{% endfor %}
如果您是itertools.groupby
用戶,請注意docs頁面中的注釋:
返回的組本身就是一個與groupby()共享基礎可迭代項的迭代器。 因為源是共享的,所以當前進groupby()對象時,先前的組不再可見。 因此, 如果以后需要該數據,則應將其存儲為列表 :
groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
(加粗體)
不要將dict
用作_grouper
,在創建此dict時,請將_grouper
值轉換為list
。 您的模板似乎沒有問題。
{% for item in dict%}
//do stuff
{% endfor %}
除非您的字典具有要迭代的嵌套屬性“項目”,否則它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.