简体   繁体   English

如何在Jinja2模板中获取列表中的列表

[英]How to get list in a list in jinja2 template

In Flask, 在烧瓶中

@app.route('/test')
def test():
    test = ['a','b','c','d','e','f','g',['1','2','3']]
    return render_template('test.html',data=test)

here is my template : 这是我的模板:

{% for item in data %}
<tr>
  <td>{{item.0}}</td> 
  <td>{{item.1}}</td>  
  <td>{{item.2}}</td>
  <td>{{item.3}}</td> 
  <td>{{item.4}}</td>  
  <td>{{item.5}}</td>
  <td>{{item.6}}</td>
</tr>
<tr>
  <td colspan="5">{{ item[7][0]}}</td>
  <td>{{ item[7][1]}}</td>
  <td>{{ item[7][2]}}</td>
</tr>
{% endfor %}

But always getting error: 但是总是出错:

jinja2.exceptions.UndefinedError: str object has no element 7

How to get the list in list? 如何获得列表中的列表?

Looks like jinja2 need string, but it can't regonize python item[7][0] format. 看起来jinja2需要字符串,但是它不能重新格式化python item[7][0]格式。 I also tried item.7.0 and item.7[0] , but same error. 我也尝试了item.7.0item.7[0] ,但是同样的错误。

Thanks! 谢谢!

You're passing in to the template a list consisting of 8 elements (7 strings and another list of strings). 您正在将由8个元素(7个字符串和另一个字符串列表)组成的列表传递到模板中。

Then in your Jinja template you're iterating over that list: 然后在Jinja模板中迭代该列表:

{% for item in data %}

However, in your loop you're trying to access each list member individually, but this doesn't make sense inside the context of the loop. 但是,在循环中,您尝试单独访问每个列表成员,但这在循环上下文中没有意义。

Your code as it stands would work without the for loop: 您的代码无需使用for循环即可运行:

<tr>                                                                            
  <td>{{data.0}}</td>                                                           
  <td>{{data.1}}</td>                                                           
  <td>{{data.2}}</td>                                                           
  <td>{{data.3}}</td>                                                           
  <td>{{data.4}}</td>                                                           
  <td>{{data.5}}</td>                                                           
  <td>{{data.6}}</td>                                                           
</tr>                                                                           
<tr>                                                                            
  <td colspan="5">{{ data[7][0]}}</td>                                          
  <td>{{ data[7][1]}}</td>                                                      
  <td>{{ data[7][2]}}</td>                                                      
</tr> 

output: 输出:

<tr>
  <td>a</td> 
  <td>b</td>  
  <td>c</td>
  <td>d</td> 
  <td>e</td>  
  <td>f</td>
  <td>g</td>
</tr>
<tr>
  <td colspan="5">1</td>
  <td>2</td>
  <td>3</td>
</tr>

Try 尝试

{% for item in data %}
    <tr>
    {% if item|string %}  #or {% if item is string %}
        <td>{{item}}</td> 
    {% else %}
        {% for item_iside in item %}
            <td>{{item_iside}}</td> 
    {% endif %}
    </tr>
{% endfor %}

Thanks! 谢谢!

I just didn't provide a good test data After Matt answer reminding me, I found I made a stupid mistake. 我只是没有提供良好的测试数据。在Matt的回答提醒我之后,我发现自己犯了一个愚蠢的错误。 after chnage test to: 经过测试后,您可以:

test =[ ['a1','b1','c1','d1','e1','f1','g1',['1','2','3']],
        ['a2','b2','c2','d2','e2','f2','g2',['2','3','4']]
        ]

the loop works now 循环现在起作用

Thank you guys. 感谢大伙们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM