繁体   English   中英

Django模板中的字典

[英]dictionaries in 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中用于字典,但在Django模板中不起作用。 如何访问字典?

您要改为遍历(键,值)对:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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