2010-06-09 80 views
1

我有嵌入对象的字典,它看起来是这样的:在Django模板迭代字典指标

notes = { 
    2009: [<Note: Test note>, <Note: Another test note>], 
    2010: [<Note: Third test note>, <Note: Fourth test note>], 
} 

我试图访问每个音符对象的Django模板内,并具有宏大时间导航到他们。总之,我不确定如何在django模板中通过索引提取。

当前模板的代码是:

<h3>Notes</h3> 
{% for year in notes %} 
    {{ year }} # Works fine 
    {% for note in notes.year %} 
     {{ note }} # Returns blank 
    {% endfor %} 
{% endfor %} 

如果我更换{%的音符notes.year%} {用%,持续注意在notes.2010%}事情做工精细,但我需要的是“2010 '是动态的。

任何建议非常感谢。

回答

8

尝试:

<h3>Notes</h3> 
{% for year, notes in notes.items %} 
    {{ year }} 
    {% for note in notes %} 
     {{ note }} 
    {% endfor %} 
{% endfor %} 
+0

热烈的掌声/起立鼓掌/抛出内裤 非常感谢。 ...我只是花了几个小时。 ;-) – PlankTon 2010-06-09 02:30:43

+0

我看了看文档,并没有意识到“.items”的重要性。感谢您指出了这一点! – 2010-08-02 18:12:23