Python 在使用jinja(烧瓶)的2个分离柱中使用2作为回路数据?

Python 在使用jinja(烧瓶)的2个分离柱中使用2作为回路数据?,python,flask,jinja2,Python,Flask,Jinja2,我有两个从视图生成的列表。我想用html将其呈现为我运行了两个for循环的表 {% for row in country %} <tr> <td>{{ row }}</td> {% endfor %} {% for count in death %} <tr> <td>{{ count }}</td> {% endfor %} {%用于国家/地区%中的行] {{row}} {%endfor%} {死

我有两个从视图生成的列表。我想用html将其呈现为我运行了两个for循环的表

{% for row in country %}
   <tr>
   <td>{{ row }}</td>
{% endfor %}
{% for count in death %}
   <tr>
   <td>{{ count }}</td>
{% endfor %}
{%用于国家/地区%中的行]
{{row}}
{%endfor%}
{死亡百分比中的计数为%}
{{count}}
{%endfor%}
乡村死亡 ★ 美国16691★ 西班牙15447★ 意大利18279

但我需要像

★ 美国16691
★ 西班牙15447

★ 意大利18279

您可以通过比较两个循环的索引(计数器)值来实现这一点。只有当它们相等时,才会在新行上渲染它们

特殊循环变量始终指向最里面的循环。如果需要访问外部环路,可以将其别名为:

{%用于国家/地区%中的行]
{%set outer_loop=loop%}
{死亡百分比中的计数为%}
{%if outer_loop.index0==loop.index0%}注意索引从0开始
★ {{row}}{{count}}
{%endif%}
{%endfor%}
{%endfor%}

这应该可以,但我还没有测试过;这大概就是这里的想法。您还可以通过阅读文档来探索可能的内容

如果您将列表合并到视图中,您的模板可能会更好

他认为:

statistics=zip(国家,死亡)

在模板中,您现在可以遍历元组列表,如下所示:

[(“美国”、“16691”)、(“西班牙”、“15447”)、(“意大利”、“18279”)]


我不确定这在jinja中是如何工作的,但它消除了嵌套for循环的需要。

对于3嵌套循环,下面是代码

{% for row in country %}
  {% set outer_loop = loop %}
  {% for confirm in confirmed %}
     {% set inner_loop = loop %}
     {% for dead in death %}
        {% if outer_loop.index0 == loop.index0 %}  {% if inner_loop.index0 == 
        loop.index0 %}
      <tr><td>{{ row }}</td> <td> {{ confirm }}</td> <td> {{ dead }} </td></tr>
     {% endif %}
     {% endfor %}
  {% endfor %}
{%用于国家/地区%中的行]
{%set outer_loop=loop%}
{已确认%%中的确认百分比}
{%set-internal_-loop=loop%}
{死亡中死亡的百分比%}
{%if-outer_-loop.index0==loop.index0%}{%if-inner_-loop.index0==
loop.index0%}
{{row}}{{confirm}{{{dead}}
{%endif%}
{%endfor%}
{%endfor%}

{%endfor%}

它为我提供了空白页。没有输出你能调试这个吗?在if语句的前面有一个
{{row}}{{count}}
来查看打印出来的内容。这将引导您正确的方向。这一行给出了输出。如果这一行{{row}{{count}}低于if stat,那么它给出了空白页,我对代码做了一个小的更改。你能试一下让我知道吗?
{% for row in country %}
  {% set outer_loop = loop %}
  {% for confirm in confirmed %}
     {% set inner_loop = loop %}
     {% for dead in death %}
        {% if outer_loop.index0 == loop.index0 %}  {% if inner_loop.index0 == 
        loop.index0 %}
      <tr><td>{{ row }}</td> <td> {{ confirm }}</td> <td> {{ dead }} </td></tr>
     {% endif %}
     {% endfor %}
  {% endfor %}