Skip to content Skip to sidebar Skip to footer

Django - Create Table From List Of List

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

Solution 1:

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


Solution 2:

You could just loop twice, once for headers once for content?

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

Post a Comment for "Django - Create Table From List Of List"