tags:

views:

67

answers:

2

Hi all, I'm having a couple of issues getting django templating for loop tag to go through this dictionary:

It is definitely being passed to the page ok as if I just do:

{% for event in events %}
   {{ event }} 
{% endfor %}

it writes 1,2,3 but when I try and do {{ event.start }} it just doesn't output anything...

    evs = {

        "1": {
            'start': '8:00:00',
            'end': '9:00:00',
            'name': 'test',
            'description': 'test',
            'image_url': 'http://test',
            'channel_url': 'http://test',
        },

        "2": {
            'start': '8:00:00',
            'end': '9:00:00',
            'name': 'test',
            'description': 'test',
            'image_url': 'http://test',
            'channel_url': 'http://test',
        },

        "3": {
            'start': '8:00:00',
            'end': '9:00:00',
            'name': 'test',
            'description': 'test',
            'image_url': 'http://test',
            'channel_url': 'http://test',
        }

    }

And this is my django code in the template:

    {% for event in events %}
            {{ event.end }}
            {{ event.name }}
            {{ event.description }}
            {{ event.image_url }}
            {{ event.channel_url }}
    {% endfor %}

Any help would be really appreciated!

Thanks

+6  A: 

If you are just iterating over events you are just iterating over the dictonary's keys; you need to iterate over the dictionary's values: {% for event in events.values %}!

lazerscience
Thank you so much!
kron
@Ignacio: I don't believe your syntax would work. It's the equivalent of events['event'].start
Ned Batchelder
+3  A: 

Well, in your case, event is the always the key of one entry (which is a string), not the object itself, so event.start cannot work.

Have look at the documentation. You could do:

{% for key, event in events.items %}
        {{ event.end }}
        {{ event.name }}
        {{ event.description }}
        {{ event.image_url }}
        {{ event.channel_url }}
{% endfor %}
Felix Kling
If events is a dictionary, this syntax does not work. You need {% for key,event in events.items %}
Ned Batchelder
@Ned Batchelder: Ah true... I just copy and pasted from the OP but in the documentation it is right...shame on me. Thanks!
Felix Kling