如何在django模板中迭代字典的列表值

时间:2022-04-30 14:30:59

I have a dictionary where value are in list

我有一本字典,里面有列表中的值

{
    'Fees': ['88000', '88000'],
    'ROll Number': ['I0', 'I1'],
    'Mark': [10, 10]
}

So I am trying to insert this data in table, SO my Django template are

所以我尝试在表中插入这些数据,所以我的Django模板是。

  <table>
      <thead>
        <tr>
        {% for k, v in loan_bank_info.items %}
            <th>{{ k }}</th>
        {% endfor %}
        </tr>
      </thead>
      <tbody>
      <tr>
      {% for k, value in loan_bank_info.items %}
        {% for v in value %}  
          <td>{{ v }}</td>
          {% endfor %}
      {% endfor %}
      </tr>
    </tbody>
  </table>

but in table value are printing as follow,

但在表值中,打印如下,

  Fees          ROll Number     Mark
  88000 88000    I0 I1           10 10

But what I want is -

但是我想要的是-

Fees            ROll Number     Mark
  88000          I0             10 
  88000          I1             10

how to iterate over list value in Django template

如何在Django模板中迭代列表值

1 个解决方案

#1


1  

You can just iterate over the list. Something like below

你可以在列表上迭代。像下面的

<tr>
      {% for k, value in loan_bank_info.items %}
        {% for v in value %}
            {% for i in v %} 
              <td>{{ i }}</td>
            {% endfor %}
          {% endfor %}
      {% endfor %}
      </tr>

#1


1  

You can just iterate over the list. Something like below

你可以在列表上迭代。像下面的

<tr>
      {% for k, value in loan_bank_info.items %}
        {% for v in value %}
            {% for i in v %} 
              <td>{{ i }}</td>
            {% endfor %}
          {% endfor %}
      {% endfor %}
      </tr>