【问题标题】:Looping through a list of dicts with Jinja for-loop使用 Jinja for-loop 遍历字典列表
【发布时间】:2019-07-18 18:47:48
【问题描述】:

我的 Jinja for 循环以正确的次数打印列表中的第一个值,但它只是没有得到正确的值。说

列表包含

harry, male, 1994
samuel, male, 1998

它会打印出来

harry, male, 1994
harry, male, 1994

我在这里做错了什么?

    {% extends "layout.html" %}

{% block main %}
    <table style="width:100%">
        <tr>
            <th> Name </th>
            <th> Nationality </th>
            <th> Gender </th>
            <th> Study </th>
            <th> Email </th>
            <th> Password </th>
        </tr>
        {% for name, nationality, gender, study, email, password in students %}
        <tr>
            <td> {{ name }} </td>
            <td> {{ nationality }} </td>
            <td> {{ gender }} </td>
            <td> {{ study }} </td>
            <td> {{ email }} </td>
            <td> {{ password }} </td>
        {% endfor %}
        </tr>

</table>

{% endblock %}

【问题讨论】:

    标签: list dictionary for-loop html-table jinja2


    【解决方案1】:

    答案是:

    {% extends "layout.html" %}
    
    {% block main %}
        <table style="width:100%">
            <tr>
                <th> Name </th>
                <th> Nationality </th>
                <th> Gender </th>
                <th> Study </th>
                <th> Email </th>
                <th> Password </th>
            </tr>
            {% for student in students %}
            <tr>
                <td> {{ student['name'] }} </td>
                <td> {{ student['nationality'] }} </td>
                <td> {{ student['gender'] }} </td>
                <td> {{ student['study'] }} </td>
                <td> {{ student['email'] }} </td>
                <td> {{ student['password'] }} </td>
            {% endfor %}
            </tr>
    
    </table>
    
    {% endblock %}
    

    同时确保 csv 文件的第一行是:

    1.name,nationality,gender,study,email,password
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-11
      • 2019-11-29
      • 2013-02-20
      相关资源
      最近更新 更多