1

I have a list l and want to want to display in html table horizontally. I am new in html and django.

l=[10,40,50]

I did the fowllowing but it displays vertically

          <table>
              <tr><th>grades</th></tr>
              {% for items in l %}
              <tr><td>{{ items }}</td></tr>
              {% endfor %}
          </table>

this is what i want to achieve where i can alsp display years:

        2019   2018    2017
grades   10     40      50

I would be grateful for any help.

1 Answer 1

1

You should not end the <tr>, which is the row, only the <td>:

<table>
    <tr><th>grades</th>
    {% for items in l %}
        <td>{{ items }}</td>
    {% endfor %}
    </tr>
</table>

If you pass the years as well, you can typeset these as well, so if the years look for example like:

years = range(2019, 2016, -1)

We can render a table like:

<table>
    <tr><th></th>
    {% for year in years %}
        <th>{{ year }}</th>
    {% endfor %}
    </tr>
    <tr><th>grades</th>
    {% for items in l %}
        <td>{{ items }}</td>
    {% endfor %}
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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