【问题标题】:Display dict index or key name in django template在 django 模板中显示字典索引或键名
【发布时间】:2018-11-02 07:58:01
【问题描述】:

如何在 Django 模板中遍历并显示字典“索引名”或“键名”?以下是上下文中的字典。

基本上这是我的字典all_options[category][sub_category][name] 的结构。 “类别、子类别和名称”是动态的。我想先显示“类别”,然后向下钻取到“子类别”,然后再向下钻取。

模板不允许使用方括号来访问 dict 属性。

提前致谢!

上下文

 'Condenser (WC)': {
    'Water Box': {
        '2MPA Condenser Water Box': {
            'option': '2MPA Condenser Water Box',
            'chillers': [{
                'chiller': 'xxxxxxxxxxx',
                'factory': '2' ,
                'option': 'WB240.2U.F2HVKA>
            }, {
                'chiller': 'xxxxxxxxxxx',
                'factory': '1' ,
                'option': 'WB088.2H.F2AYFA>
            }]
        },

    },
    'Anodes': {
        'Magnesium Anodes': {
            'option': 'Magnesium Anodes',
            'chillers': [{
                'chiller': 'xxxxxxxxxxx',
                'factory': '2' ,
                'option': 'WB240.2U.F2HVKA>
            }, {
                'chiller': 'xxxxxxxxxxx',
                'factory': '2' ,
                'option': 'WB240.2U.F2HVKA>
            }]
        }
    },
    'Stainless Steel Tube Sheet': {
        '304 SS Condenser Tube Sheets': {
            'option': '304 SS Condenser Tube Sheets',
            'chillers': [{
                'chiller': 'xxxxxxxxxxx',
                'factory': '2' ,
                'option': 'WB240.2U.F2HVKA>
            }]
        },

    }
},

模板

在模板中,我添加了一个注释,它是需要打印的字符串。

 {% for category_name in all_options %}
    {{ category_name }} #Condenser (WC)

{% for subcat in category_name %}
    {{ subcat }} #Water Box

    {% for item in subcat %}
        {{ item }}  #2MPA Condenser Marine Water Box

         {% for chiller in item.chillers %}
            {{ chiller.option }}  #WB200.3K.F2HVKA

        {% endfor%}

    {% endfor%}

{% endfor%}

{% endfor %}

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    在 Python 中,您可以通过调用字典上的 .items() 函数来迭代可迭代的 if 2-tuples。在 Django 模板中,我们也可以这样做:

    {% for category_name, category in all_options.items %}
        {{ category_name }} #Condenser (WC)
    
    {% for subcat_name, subcat in category.items %}
        {{ subcat_name }} #Water Box
    
        {% for item_name, item in subcat.items %}
            {{ item_name }}  #2MPA Condenser Marine Water Box
    
             {% for chiller in item.chillers %}
                {{ chiller.option }}  #WB200.3K.F2HVKA
    
            {% endfor%}
    
        {% endfor%}
    
    {% endfor%}
    
    {% endfor %}

    (或类似的东西)

    所以这里category_name 是与字典项关联的category(所以在这种情况下也是字典*) .然后,您可以再次枚举该字典,依此类推。

    请注意,在 Python 中字典是无序:这意味着迭代可以以任何可能的顺序发生。如果您需要固定顺序,我建议您使用 2 元组列表,在这种情况下,您当然不必致电 .items。此外,字典只能包含 hashable 键,并且每个键最多可以出现 一次。这不是因为 Django,而是因为 Python 中的字典是如何设计的。

    如前所述,如果您想要一个有序的元素集合,这样“键”不必是可散列的和/或多次出现,我建议您使用 2 元组列表(类似于[(k1, v1), (k2, v2)]ki 键,vi 对应的值)。

    如果您对这些值感兴趣,您可以使用.values,它会在字典的上生成一个可迭代对象。

    【讨论】:

    • 哇!像魅力一样工作!!!!我已经在创建自定义模板标签。非常感谢!
    猜你喜欢
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多