如何在django模板中的字典中迭代字典? [英] how to iterate through dictionary in a dictionary in django template?

查看:330
本文介绍了如何在django模板中的字典中迭代字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字典看起来像这样(字典中的字典):

My dictionary looks like this(Dictionary within a dictionary):


{'0':{'selected_unit' ':十进制('10.0000'),
'unit__name_abbrev':u'G','supplier_ 供应商':uSteve's Meat
Locker,'price':Decimal('5.00' ),'供应商
_地址':
u'No\r\\\
address here','selected_unit_amount':u'2','city_ name':
u' Joburg,Central','供应商
_phone_number':u'02299944444',
'supplier_ 网站':无,'供应商 _price_list':u'',
'supplier_ email':u'ss.sss@ssssss.com','unit _name':u'Gram',
'name':u'Rump Bone'}}

{'0': {'chosen_unit': , 'cost': Decimal('10.0000'), 'unit__name_abbrev': u'G', 'supplier_supplier': u"Steve's Meat Locker", 'price': Decimal('5.00'), 'supplier_address': u'No\r\naddress here', 'chosen_unit_amount': u'2', 'city_name': u'Joburg, Central', 'supplier_phone_number': u'02299944444', 'supplier_website': None, 'supplier_price_list': u'', 'supplier_email': u'ss.sss@ssssss.com', 'unit_name': u'Gram', 'name': u'Rump Bone'}}

现在我只是试图在我的模板上显示信息,但我正在努力。我的模板代码如下:

Now I'm just trying to display the information on my template but I'm struggling. My code for the template looks like:

        {% if landing_dict.ingredients %}
            <hr>
            {% for ingredient in landing_dict.ingredients %}
                {{ ingredient }}
            {% endfor %}
            <a href="/">Print {{ landing_dict.recipe_name }}</a>
        {% else %}
            Please search for an ingredient below
        {% endif %}

它只是在我的模板上显示'0'?

It just shows me '0' on my template?

我也试过:

            {% for ingredient in landing_dict.ingredients %}
                {{ ingredient.cost }}
            {% endfor %}

这甚至不显示结果。

我以为也许我需要迭代一级更深,所以尝试这样:

I thought perhaps I need to iterate one level deeper so tried this:

        {% if landing_dict.ingredients %}
            <hr>
            {% for ingredient in landing_dict.ingredients %}
                {% for field in ingredient %}
                    {{ field }}
                {% endfor %}
            {% endfor %}
            <a href="/">Print {{ landing_dict.recipe_name }}</a>
        {% else %}
            Please search for an ingredient below
        {% endif %}

但是这不显示任何东西。

But this doesn't display anything.

我做错了什么?

推荐答案

让你的数据是 -

data = {'a':[[1,2 ]],'b':[[3,4]],'c':[[5,6]]}

可以使用 data.items()方法来获取字典元素。请注意,在django模板中,我们不会将()。还有一些用户提到的值[0] 不起作用,如果是这样,那么尝试 values.items 。 / p>

You can use the data.items() method to get the dictionary elements. Note, in django templates we do NOT put (). Also some users mentioned values[0] does not work, if that is the case then try values.items.

<table>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>

    {% for key, values in data.items %}
    <tr>
        <td>{{key}}</td>
        {% for v in values[0] %}
        <td>{{v}}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

我很确定你可以将这个逻辑扩展到你的具体dict。

Am pretty sure you can extend this logic to your specific dict.

以排序顺序迭代dict键 - 首先我们在python中进行迭代,然后迭代&在django模板中渲染

To iterate over dict keys in a sorted order - First we sort in python then iterate & render in django template.

返回render_to_response('some_page.html',{'data':sorted(data.items())})

在模板文件中:

{% for key, value in data %}
    <tr>
        <td> Key: {{ key }} </td> 
        <td> Value: {{ value }} </td>
    </tr>
{% endfor %}

这篇关于如何在django模板中的字典中迭代字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆