繁体   English   中英

通过Django模板中的列表字典进行处理

[英]processing through a dictionary of lists in a Django template

我有一本字典,看起来像这样:list = {item:[thing,thing,thing,thing],item:[thing,thing]}

我目前正在尝试显示所有项目和类似内容:

Item

    thing
    thing
    thing

Item

    thing
    thing

我试过了

{% for item, things in list %}
    -{{Item}}
    {% for thing in things %}
        {{thing}}
    {% endfor %}
{% endfor %}

但是我的输出看起来像

-

-

-

-

-

我以前尝试过

{% for item in list %}
    -{{item}}
    {% for thing in list[item] %}
        {{thing}}
    {% endfor %}
{% endfor %}

这根本没有用。

您应该使用list.items来指定要遍历(键,值)元组而不是键(就像在Python代码中那样)。

另外,如果您希望输出内容更具可读性,则必须包含一些换行符。

这样的事情应该起作用:

{% for item_name, things in list.items %}
    -{{item_name}}<br />
    {% for thing in things %}
        {{thing}}<br />
    {% endfor %}
{% endfor %}

暂无
暂无

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

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