Below the Radar
Below the Radar

Reputation: 7635

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 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.

Upvotes: 1

Views: 4272

Answers (2)

JamesO
JamesO

Reputation: 25966

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>

Upvotes: 1

Ngenator
Ngenator

Reputation: 11269

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

Upvotes: 2

Related Questions