Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python jinja2烧瓶:迭代时使用rowspan两次_Python_Html_Flask_Jinja2 - Fatal编程技术网

Python jinja2烧瓶:迭代时使用rowspan两次

Python jinja2烧瓶:迭代时使用rowspan两次,python,html,flask,jinja2,Python,Html,Flask,Jinja2,我通过flask将以下列表传递给Jinja2模板: yrs = [2016, 2017] months = [['June'], ['January', 'February']] names = [[['John', 'Mike']], [['Sara'], ['Steph', 'James']]] 我正试着做一张像这样的桌子: 以下是我为实现这一目标而编写的不成功的Jinja2代码: <div class="table-responsive"> <table class

我通过flask将以下列表传递给Jinja2模板:

yrs = [2016, 2017]
months = [['June'], ['January', 'February']]
names = [[['John', 'Mike']], [['Sara'], ['Steph', 'James']]]
我正试着做一张像这样的桌子:

以下是我为实现这一目标而编写的不成功的Jinja2代码:

<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Year</th>
        <th>Month</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>
      {% for yr_index in range(yrs | count) %}
        {% for month_index in range(months[yr_index] | count) %}
          {% for name_index in range(names[yr_index][month_index] | count) %}
            <tr>
              {% if loop.first %}
                {% if loop.first %}
                  <td rowspan="{{ names[yr_index] | count }}">{{ yrs[yrs_index] }}</td>
                {% endif %}
              <td rowspan="{{ names[yr_index][month_index] | count }}">{{ months[yr_index][month_index] }}</td>
              {% endif %}
              <td>{{ names[yr_index][month_index][name_index] }}</td>
            </tr>
        {% endfor %}
      {% endfor %}
    </tbody>
  </table>
<div>

年
月
名称
{范围内的yr|U指数百分比(yrs|计数)%}
{范围内的月指数百分比(月[yr_指数]|计数)%}
{范围内名称索引的百分比(名称[年索引][月索引]|计数)%}
{%if loop.first%}
{%if loop.first%}
{{yrs[yrs_index]}
{%endif%}
{{月[年指数][月指数]}
{%endif%}
{{names[yr_index][month_index][name_index]}
{%endfor%}
{%endfor%}
因此,我基本上只是尝试将这些列表解压到一个有组织的表中,但是我在
rowspan
属性方面遇到了很多问题。任何提示都将不胜感激,即使它与更改python列表的结构有关。

请先尝试一下(未测试)。似乎您误用了
循环。first
(您有嵌套的循环,实际上不能依赖
循环。first
)。在检索计数之前,您还需要展平
名称[yr\u index]
列表

请考虑阅读以下内容:


年
月
名称
{yr的%yrs%}
{%set yr_loop=loop%}
{月份中月份的百分比[loop.index0]}
{%set MOUNT_loop=loop%}
{%表示名称[yr_loop.index0][loop.index0]]}
{%if loop.first%}
{%if month_loop.first%}
{{yr}}
{%endif%}
{{month}
{%endif%}
{{name}}
{%endfor%}
{%endfor%}

我收到以下错误:“UndefinedError:‘列表对象’没有属性‘值’”。看来我需要弄清楚如何计算每年的项目数。访问父循环的好技巧-应该可以@jcmetz21只需删除
.values()
。我的错误。
<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Year</th>
        <th>Month</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>
      {% for yr in yrs %}
        {% set yr_loop = loop %}
        {% for month in months[loop.index0] %}
          {% set month_loop = loop %}
          {% for name in names[yr_loop.index0][loop.index0] %}
            <tr>
              {% if loop.first %}
                {% if month_loop.first %}
                  <td rowspan="{{ names[yr_loop.index0]|sum(start=[])|count }}">{{ yr }}</td>
                {% endif %}
                <td rowspan="{{ loop.length }}">{{ month }}</td>
              {% endif %}
              <td>{{ name }}</td>
            </tr>
        {% endfor %}
      {% endfor %}
    </tbody>
  </table>
<div>