views:

103

answers:

1

Hi folks...I have a dictionary with embedded objects, which looks something like this:

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

I'm trying to access each of the note objects inside a django template, and having a helluva time navigating to them. In short, I'm not sure how to extract by index in django templating.

Current template code is:

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

If I replace {% for note in notes.year %} with {% for note in notes.2010 %} things work fine, but I need that '2010' to be dynamic.

Any suggestions much appreciated.

+4  A: 

Try:

<h3>Notes</h3>
{% for year, notes in notes.items %}
    {{ year }}
    {% for note in notes %}
        {{ note }}
    {% endfor %}
{% endfor %}
rz
rapturous applause/standing ovation/throws underpantsMany thanks. ...I just spent hours on that. ;-)
unclaimedbaggage
glad to help. feel free to accept it ;-)
rz
I looked at the documentation and didn't recognise the importance of ".items". Thanks for pointing this out!
Daniel Quinn