python - 在 Django 模板中动态循环列表项的列表
问题描述
我在一个看起来像这样的列表中有一个列表:
clientList = [['Client 1', 'Yes', 'No', 'Yes', 'Yes'], ['Client 2', 'No', 'No', 'Yes', 'Yes'], ['Client 3', 'Yes', 'Yes', 'Yes', 'Yes']]
我需要动态调用模板中的列表,如下所示
<table>
<tr>
{% for c in clientList %}
<td>{{c}}</td>
{% endfor %}
<tr>
<table>
但它不起作用,因为它看起来像这样:
而且我也不能使用该方法循环它,{{c.0}}, {{c.1}}, {{c.3}}, {{c.4}}
因为列表会根据选择的客户端数量而变化。所以我需要动态循环它。
我尝试使用此链接中的方法,但它不起作用,因为我一直收到错误list indices must be integers or slices, not str
有什么办法可以做到这一点?
解决方案
请尝试以下。正如你所说,列表中有一个列表。您确实遍历了第一个列表,但忘记了第二个。
<table>
<tr>
{% for c in clientList %}
{% for a in c %}
<td>{{a}}</td>
{% endfor %}
{% endfor %}
<tr>
<table>
推荐阅读
- javascript - Gulp (gulp.dest() not working) 没有创建文件夹,也没有放置任何文件
- vue.js - 子组件、VueJs 和 BootstrapVue 中的 Popover Flash
- mongodb - MongoDB聚合展开、匹配、分组查询组合
- jmeter - ApacheJMeter 动态负载测试
- javascript - 在 Stripe 中创建客户时出现无效请求错误
- reactjs - Chrome 扩展:在服务器端页面中按属性查找元素
- javascript - jquery set 函数对每个动态添加的输入 id 做出反应并获取此输入 id 号
- azure - Syslog 应用程序作为无服务器
- php - 使用 kreait/firebase-php 时未定义的类常量 'VERSION'
- haskell - 在 Nat 上使用 * 作为原语