FLASK:从 python 字典在 JINJA 中创建 table

FLASK: Creating table in JINJA from python dict

我正在尝试根据字典在 HTML 中构建 table

我有以下数据:

alternativas = { 'attribute1':[val1, val2, val3] , 'attribute2':[val4, val5]}

我想显示一个 table 比如:

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 循环遍历字典有时很棘手