2010-06-09 106 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