如何在django模板中的字典中遍历字典?

时间:2021-07-28 20:19:25

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

我的字典是这样的(字典里的字典):

{'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'}}

{' style ' font - family: " tahoma ", " sans - serif "; mso - fareast - font - family:宋体;mso - bidi - font - family: " times new roman "; mso - bidi - theme - font: minor - bidi ' > < span

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

It just shows me '0' on my template?

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

I also tried:

我也试过:

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

This doesn't even display a result.

这甚至不会显示结果。

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.

但是这个没有显示任何东西。

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


174  

Lets say your data is -

假设你的数据是-

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

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

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.

可以使用data.items()方法获取字典元素。注意,在django模板中我们不放置()。还有一些用户提到的值[0]不起作用,如果是这样,请尝试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>

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

我非常确信您可以将这种逻辑扩展到您的特定命令。


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

要以排序的顺序遍历字典键——首先在python中排序,然后在django模板中迭代和呈现。

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

返回render_to_response(some_page。html的,{“数据”:排序(data.items())})

In template file:

在模板文件:

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

#1


174  

Lets say your data is -

假设你的数据是-

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

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

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.

可以使用data.items()方法获取字典元素。注意,在django模板中我们不放置()。还有一些用户提到的值[0]不起作用,如果是这样,请尝试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>

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

我非常确信您可以将这种逻辑扩展到您的特定命令。


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

要以排序的顺序遍历字典键——首先在python中排序,然后在django模板中迭代和呈现。

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

返回render_to_response(some_page。html的,{“数据”:排序(data.items())})

In template file:

在模板文件:

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