我正在尝试从dict.构建HTML中的table
我有以下数据:
alternativas = { 'attribute1':[val1, val2, val3] , 'attribute2':[val4, val5]}
我想显示一个表格,比如:
attribute1 attribute2
val1 val4
val2 val5
val3
我尝试过:
{% for dict_item in alternativas %}
<tr>
{% for value in alternativas.values() %}
{% for item in value %}
<td> {{ item }} </td>
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
但是我不能得到我想要的结果。
正确的代码应该是(修改后的行有注释(:
{% for dict_item in alternativas %}
<tr>
{% for value in alternativas[dict_item] %} #amended line
{% for item in value %}
<td> {{ item }} </td>
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
或者更简洁地说:
{% for key, value in alternativas %}
<tr>
{% for item in value %}
<td> {{ item }} </td>
{% endfor %}
</tr>
{% endfor %}
但不确定它是否正常工作,因为jinja在字典中循环有时很棘手