1

I am listing down the data coming from the database in a table. But it is not aligning as a table.

what I want to display in the web page is,

Name       john mark 
Faculty    cs 
University xxx 

But what I get in the web page is,

Name john mark 
Faculty cs 
University xxx

In my .html I have,

{% for item in data %}
<tr>
    <td>{{item[0]}}</td>
    <td>{{item[1]}}</td>
</tr>
<br>
{% endfor %}

Please help me with this as I am new to python flask.

5
  • Have you wrapped tr with table element? Commented May 21, 2018 at 3:35
  • No my .html only has what i have mentioned above.Do I have to wrapped it with a table element? Commented May 21, 2018 at 3:39
  • 1
    Yes, off course. You have to make a valid table structure. Then it will rendered correctly. Commented May 21, 2018 at 3:50
  • Yes it helped. Thank you @BhushanBabar Commented May 21, 2018 at 3:57
  • Hi, Kindly upvote and accept the answer if it really helped. Thanks. Commented May 21, 2018 at 5:02

2 Answers 2

5

This is how valid table HTML should look. You have not wrapped tr with <table> element.

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>

So your code should look like this.

<table>
{% for item in data %}
<tr>
    <td>{{item[0]}}</td>
    <td>{{item[1]}}</td>
</tr>
{% endfor %}
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think that there is any need for br tag inside the table.
@NanditaAroraSharma Yes, not needed but that does not break the table, I have tested, besides that was not the problem that caused problems to OP.
Agreed it doesnt cause any issue but it may cause issues in IE :) So we should avoid using anything that is not according to table markup.
@NanditaAroraSharma Yes, I had noticed it , but it got copy pasted . Thanks for pointing it.
2

Try this html

<table>
    <tbody>
    {% for item in data %}
    <tr>
        <td>{{item[0]}}</td>
        <td>{{item[1]}}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>

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.