在django模板中迭代字典索引

Pla*_*Ton 1 python django django-templates

我有一个嵌入对象的字典,看起来像这样:

notes = {
    2009: [<Note: Test note>, <Note: Another test note>],
    2010: [<Note: Third test note>, <Note: Fourth test note>],
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试访问django模板中的每个注释对象,并且有一个helluva时间导航到它们.简而言之,我不确定如何在django模板中通过索引提取.

目前的模板代码是:

<h3>Notes</h3>
{% for year in notes %}
    {{ year }} # Works fine
    {% for note in notes.year %}
        {{ note }} # Returns blank
    {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

如果我将{%for notes inye.yar%}替换为{%for note in notes.2010%},一切正常,但我需要'2010'是动态的.

任何建议非常感谢.

rz.*_*rz. 8

尝试:

<h3>Notes</h3>
{% for year, notes in notes.items %}
    {{ year }}
    {% for note in notes %}
        {{ note }}
    {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)