django模板中的字典

时间:2014-02-25 11:32:01

标签: python django dictionary django-templates

我有一个可以包含以下值的字典

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

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

我希望单独访问这些值,最后是每个标题的对象列表。我的代码是

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

但它不起作用。它适用于python中的dictionalry但不适用于django模板。我怎样才能访问字典?

1 个答案:

答案 0 :(得分:1)

您希望迭代(键,值)对:

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