2

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

2 Answers 2

6

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 %}!

Sign up to request clarification or add additional context in comments.

2 Comments

@Ignacio: I don't believe your syntax would work. It's the equivalent of events['event'].start
The syntax crashes my code. However, {% for key, event in events.items %} does not work either but doesnt crash my code. Why am I not able to print {{event}} to the page?
5

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 %}

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.