簡體   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