1

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.

3
  • You have a <th> within <td> which is not right. What does {{attributes}} contain? Commented Apr 24, 2013 at 14:18
  • {{attributes}} is a list of list [['header','value'], ... ]] Commented Apr 24, 2013 at 14:19
  • possible duplicate of How to create a recursive table in HTML Commented Apr 24, 2013 at 22:37

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking about using two loops like your proposal, but I wonder if its possible to do the same within a single loop?
Not really in this case. If you were to render many rows and 2 columns, it would be very simple though
Ok thank you. I will accept yours, because you were the first to answer.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.