防止烧瓶在循环中打印新线

时间:2017-02-11 20:12:53

标签: python sqlite flask jinja2

我正在尝试使用flash中的循环生成表格标题但是我在每个之后都获得了新的行:

{% extends "layout.html" %}

{% block title %}
    Portfolio overview
{% endblock %}

{% block main %}

{% for variable in variables -%}
    {% for key, value in variable.items() -%}
    <table>
       <thead><th>{{ key }}</th></thead>
    {% endfor -%}
    {% for key, value in variable.items() -%}    
    <tbody>    
    <td>{{ value }}</td>
    </tbody>            
    {% endfor -%}
{%- endfor -%}
    </table>
<h2>Cash available (US$): {{ cash }}</h2>
<h2>Total portfolio value (US$): {{ wealth }}</h2>    

{% endblock %}

Result

但是,我想得到这个 Desired result

1 个答案:

答案 0 :(得分:0)

给这一点;

{% extends "layout.html" %}

{% block title %}
    Portfolio overview
{% endblock %}

{% block main %}

<table>
    {% for variable in variables %}
        <thead>
            {% for key, value in variable.items() %}
                <th>{{ key }}</th>
            {% endfor %}
        </thead>
        <tbody>
            {% for key, value in variable.items() %}    
                <td>{{ value }}</td>
            {% endfor %}
        </tbody>            
    {% endfor %}
</table>
<h2>Cash available (US$): {{ cash }}</h2>
<h2>Total portfolio value (US$): {{ wealth }}</h2>    

{% endblock %}

我刚刚修复了for循环的位置。