ChatGPT解决这个技术问题 Extra ChatGPT

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

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

{'0': {
    'chosen_unit': <Unit: Kg>,
    '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',
}}

现在我只是想在我的模板上显示信息,但我很挣扎。我的模板代码如下所示:

{% 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”?

我也试过:

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

这甚至不显示结果。

我想也许我需要更深入地迭代一个级别,所以尝试了这个:

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

但这并没有显示任何东西。

我究竟做错了什么?


S
Srikar Appalaraju

假设您的数据是 -

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

您可以使用 data.items() 方法来获取字典元素。请注意,在 django 模板中,我们不放置 ()。还有一些用户提到 values[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>

我很确定您可以将此逻辑扩展到您的特定字典。

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

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

感谢您的回答。我将 recipe_name 提升了一级,但没有显示该级别的字典。谢谢您的回答!我不能使用 values[0] 而必须使用 values.items
凉爽的!很高兴分享我所知道的。代码是自由输入的,所以一些错误是不可避免的。
感谢您提及 .items。文档 https://docs.djangoproject.com/en/1.4/topics/templates/ 给出了一个不起作用的例子,但没有一个例子起作用。 {% for k,v in dict %} 给出了奇怪的结果 - k 是每个键的第一个字符,v 是空白,而 {% for k in dict %} 返回完整的键但无法检索值(因为 dict.k 将 k 视为文字字符)。
赞成:-)。对于未来的读者,标签和过滤器参考 https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for 记录了这一点,以及在格式化列表时可能有用的 forloop 变量。
values.items +1
d
daaawx

这个答案对我不起作用,但我自己找到了答案。但是,没有人发布我的问题。懒得问再回答,就放在这里吧。

这是针对以下查询:

data = Leaderboard.objects.filter(id=custom_user.id).values(
    'value1',
    'value2',
    'value3')

在模板中:

{% for dictionary in data %}
  {% for key, value in dictionary.items %}
    <p>{{ key }} : {{ value }}</p>
  {% endfor %}
{% endfor %}

E
Eugene Chabanov

如果您将变量 data(字典类型)作为上下文传递给模板,那么您的代码应该是:

{% for key, value in data.items %}
    <p>{{ key }} : {{ value }}</p> 
{% endfor %}