简体   繁体   中英

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]}

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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