Django:具有迭代列表值的HTML Table [英] Django : HTML Table with iterative lists values

查看:33
本文介绍了Django:具有迭代列表值的HTML Table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django框架,并尝试根据列表中的值构建表:

I am using Django framework and trying to build a table based on the values i have in a list:

列表: [[1、2、3、4],[5、6、7、8],[9、10、11、12],[3、3、3、3]] 表格应该如下所示:

list : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [3, 3, 3, 3]] The table should be as shown below:

我使用的代码是:

  html = "<html><body><br>" \
           "<table border=1 ><thead> <tr><th>result 1</th><th>result 2</th><th>result 3</th><th>result 4</th></tr><thead>{% for item in matrix %} {% for secitem in item %} <tr> <td> {{secitem[0]}} </td>  <td> {{secitem[1]}} </td><td> {{secitem[2]}} </td><td> {{secitem[3]}} </td>  </tr>    {% endfor %}{% endfor %} " \
           "</table></body></html>"

我无法获得包含所需行的确切表.非常感谢您的帮助.![在此处输入图片描述] [1]

I am not able to get the exact table with the required rows in it. Any help is highly appreciated.![enter image description here][1]

推荐答案

您正在遍历列表中的列表,但是您正在尝试访问列表中列表中项目的索引.

You are iterating over a list within a list, yet you are trying to access indices of an item within a list within a list.

删除"secitem"循环并访问 item 的索引,或者简单地遍历 item .

Remove the "secitem" loop and access indices of item OR simply iterate over the item.

{% for row in matrix %}
<tr>
    {% for value in row %}
        <td>{{ value }}</td>
    {% endfor %}
</tr>
{% endfor %}

这篇关于Django:具有迭代列表值的HTML Table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆