<small id='YdG7q'></small><noframes id='YdG7q'>

      <bdo id='YdG7q'></bdo><ul id='YdG7q'></ul>

    <legend id='YdG7q'><style id='YdG7q'><dir id='YdG7q'><q id='YdG7q'></q></dir></style></legend>

      <tfoot id='YdG7q'></tfoot>
    1. <i id='YdG7q'><tr id='YdG7q'><dt id='YdG7q'><q id='YdG7q'><span id='YdG7q'><b id='YdG7q'><form id='YdG7q'><ins id='YdG7q'></ins><ul id='YdG7q'></ul><sub id='YdG7q'></sub></form><legend id='YdG7q'></legend><bdo id='YdG7q'><pre id='YdG7q'><center id='YdG7q'></center></pre></bdo></b><th id='YdG7q'></th></span></q></dt></tr></i><div id='YdG7q'><tfoot id='YdG7q'></tfoot><dl id='YdG7q'><fieldset id='YdG7q'></fieldset></dl></div>
      1. 如何在 django 模板中的字典中遍历字典?

        时间:2024-04-21
          <tbody id='I88QF'></tbody>
        1. <i id='I88QF'><tr id='I88QF'><dt id='I88QF'><q id='I88QF'><span id='I88QF'><b id='I88QF'><form id='I88QF'><ins id='I88QF'></ins><ul id='I88QF'></ul><sub id='I88QF'></sub></form><legend id='I88QF'></legend><bdo id='I88QF'><pre id='I88QF'><center id='I88QF'></center></pre></bdo></b><th id='I88QF'></th></span></q></dt></tr></i><div id='I88QF'><tfoot id='I88QF'></tfoot><dl id='I88QF'><fieldset id='I88QF'></fieldset></dl></div>
          • <bdo id='I88QF'></bdo><ul id='I88QF'></ul>
          • <small id='I88QF'></small><noframes id='I88QF'>

              <tfoot id='I88QF'></tfoot>

              • <legend id='I88QF'><style id='I88QF'><dir id='I88QF'><q id='I88QF'></q></dir></style></legend>
                  本文介绍了如何在 django 模板中的字典中遍历字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  {'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
                  address 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 模板中,我们不会放置 ().还有一些用户提到 values[0] 不起作用,如果是这种情况,请尝试 values.items.

                  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>
                  

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

                  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.

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

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

                  上一篇:如何在 Django 查询集过滤中执行不等于? 下一篇:如何在 Django 中创建 slug?

                  相关文章

                • <i id='8dVE4'><tr id='8dVE4'><dt id='8dVE4'><q id='8dVE4'><span id='8dVE4'><b id='8dVE4'><form id='8dVE4'><ins id='8dVE4'></ins><ul id='8dVE4'></ul><sub id='8dVE4'></sub></form><legend id='8dVE4'></legend><bdo id='8dVE4'><pre id='8dVE4'><center id='8dVE4'></center></pre></bdo></b><th id='8dVE4'></th></span></q></dt></tr></i><div id='8dVE4'><tfoot id='8dVE4'></tfoot><dl id='8dVE4'><fieldset id='8dVE4'></fieldset></dl></div>

                    • <bdo id='8dVE4'></bdo><ul id='8dVE4'></ul>
                    <legend id='8dVE4'><style id='8dVE4'><dir id='8dVE4'><q id='8dVE4'></q></dir></style></legend>
                    <tfoot id='8dVE4'></tfoot>
                  1. <small id='8dVE4'></small><noframes id='8dVE4'>