简体   繁体   中英

dictionaries in django templates

I have a dictionary that could consist of the following values

images={'25/02/2014': {u'Observation': [<Image: Image object>]}} 

that is

{
    date:{title1:[obj1, obj2, obj3], title2:[obj1, obj2, obj3]},
    date2:{title1:[obj1, obj2, obj3]},
}

and I want to access the values individually and eventually the object list for each title. My code was

{%for date in images%}
    {%for title in images.date%} #equivalent to for title in images[date]:
        {{date}} - {{title}}
        {% for obj in images.date.title%} #equivalent to for obj in images[date][title]: but not sure
            {{obj}}
        {%endfor%}
    {%endfor%}
{%endfor%}

but it won't work. It works in python for a dictionalry but not in django template. How can I access a dictionary?

You want to iterate on the (key, values) pairs instead:

{% for date, data in images.items %}
  {% for title, images in data.items %} 
    {{date}} - {{title}}
    {% for image in images %} 
      {{ image }}
    {% endfor %}
  {% endfor %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM