Django - 从列表列表创建表 [英] Django - Create table from list of list

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

问题描述



我通过列表我的表格不是很好,所以这个问题可能很简单。 {{attributes}} 到模板,我想创建一个包含2行和多列的表。



模板: / strong>

 < div id =table> 
< table border = 0>
{%for attr in attributes%}
< td>
< th> {{attr.0}}< / th>
{{attr.1}}
< / td>
{%endfor%}
< / table>
< / div>

我想要 {{attr.0}} 作为标题并显示在单个行上,第二行显示 {{attr.1}}

解决方案

如何

 < div id =table> ; 
< table border = 0>
< thead>
< tr>
{%for attr_head in attributes.keys%}
< th> {{attr_head}}< / th>
{%endfor%}
< / tr>
< / thead>
< tbody>
< tr>
{%for attr in attributes.values%}
< td> {{attr}}< / td>
{%endfor%}
< / tr>
< / tbody>
< / table>
< / div>

只需循环通过dict的键并将其呈现为 th 元素,然后循环遍历值,将它们呈现在 tbody 中。 th td 是表中的列, tr



此外,您应该阅读 html表,他们不是那么难


I am not very good with table in html, so this question might be very simple to answer.

I pass the list of list {{ attributes }} to the template and I want to create a table with 2 rows and many columns.

TEMPLATE:

<div id="table">
<table border=0>
{% for attr in attributes %}
    <td>
       <th>{{ attr.0 }}</th>
        {{ attr.1 }}
    </td>
{% endfor %}
</table>
</div>

I want the {{ attr.0 }} to be the header and displayed on a single row and the {{ attr.1 }} displayed on the second row.

解决方案

How about

<div id="table">
<table border=0>
<thead>
    <tr>
    {% for attr_head in attributes.keys %}
       <th>{{ attr_head }}</th>
    {% endfor %}
    </tr>
</thead>
<tbody>
    <tr>
    {% for attr in attributes.values %}
        <td>{{ attr }}</td>
    {% endfor %}
    </tr>
</tbody>
</table>
</div>

Just loop through the dict's keys and rendering them as th elements in the table header and then loop through the values, rendering them inside the tbody. th and td are columns in the table and tr are rows.

Also, you should read up on html tables, they aren't that hard

这篇关于Django - 从列表列表创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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