2012-06-29 204 views
0

在Django中,我有一本字典,其中包含词典,例如:遍历Django的字典

d['dict1'] = [('1', 'some information'), ('2', 'some information')] 
d['dict2'] = [('1', 'some more information'), ('2', 'some more information')] 

,我需要遍历它,但每次它遍历只抢到dict1的价值和dict2与loop.counter对应,所以例如

first time through it would output 
1 3 
and then 
2 4 
and so on and so forth 

我试图做:

{% for item1, item2 in d.items %} 
{{ item1.forloop.counter.value1 }} 
{{ item2.forloop.counter.value1 }} 
{% endfor %} 

它什么都不产生。

编辑:我更新了一下字典实际上看起来像

回答

2

你应该把这样的:

d['dict1'] = [('value1', '1'), ('value2', '2')] 
d['dict2'] = [('value1', '3'), ('value2', '4')] 

到这一点:

result = [('value1', '1', '3'), ('value2', '2', '4')] 

您可以在您的视图做到这一点。你基本上准备你的数据显示在模板中。

然后,您可以轻松地遍历值:

{% for name, v1, v2 in result %} 
{{ v1 }} 
{{ v2 }} 
{% endfor %} 
+0

还好,但我的问题是,我该怎么做,在我看来,它使用一个循环来获取所有necesary对象,并把它放在字典(ies) – flaiks

+0

请提供您的视图中的代码。 –