【问题标题】:dictionaries in django templatesdjango 模板中的字典
【发布时间】:2014-03-27 14:26:52
【问题描述】:

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

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 模板。如何访问字典?

【问题讨论】:

标签: python django dictionary django-templates


【解决方案1】:

您想要迭代 (key, values) 对:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2019-10-22
    • 2018-05-24
    • 2014-03-04
    • 2017-11-07
    • 2014-10-11
    相关资源
    最近更新 更多