简体   繁体   English

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

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

I have a dictionary which looks like this: list = {item : [thing, thing, thing, thing], item : [thing, thing]} 我有一本字典,看起来像这样:list = {item:[thing,thing,thing,thing],item:[thing,thing]}

I am currently trying to display all items and things like so: 我目前正在尝试显示所有项目和类似内容:

Item

    thing
    thing
    thing

Item

    thing
    thing

I tried 我试过了

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

But my output looks kinda like 但是我的输出看起来像

-

-

-

-

-

I have previously tried 我以前尝试过

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

which didn't work at all. 这根本没有用。

You should use list.items to specify that you want to iterate over the (key, value) tuples and not over the keys (like you would do in Python code anyways). 您应该使用list.items来指定要遍历(键,值)元组而不是键(就像在Python代码中那样)。

Also you'll have to include some line breaks if you want your output to be somehow more readable. 另外,如果您希望输出内容更具可读性,则必须包含一些换行符。

Something like this should work: 这样的事情应该起作用:

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