0

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

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

The code I have used is :

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

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]

2
  • Your example table is invisible. Commented Jun 29, 2015 at 20:42
  • I am unable to upload image. However, it should be a 4X4 table with values of list present. Commented Jun 29, 2015 at 20:50

1 Answer 1

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.

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 %}
Sign up to request clarification or add additional context in comments.

9 Comments

Is there something wrong with this code? I am still unable to get the result in tables: 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><th>result 5</th></tr><thead>{% for row in matrix %}<tr>{% for value in row %}<td>{{ value }}</td>{% endfor %}</tr>{% endfor %} " \ "</table></body></html>"
or, if you want the headers: <table border=1><tr><th>Col1</th><th>Col2</th><th>Col3</th><th>Col4</th></tr> suggested code </table>
@GoutamAdwant that is correct. What is the output and issues you're still having? Note: if <thead> is present, you should add <tbody> around your primary rows.
@Yuji'Tomita'Tomita I have removed <thead> from the code. The output i am getting is this : s15.postimg.org/vfjpc7597/image.png
@GoutamAdwant to me that looks like you need to compile your template. Rendering a django template in a view should be a separate issue altogether that I assumed you had already accomplished. Are you doing so?
|

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.