Python list(s) into html table - python

I'm trying to get my display my list(s) into a html table but it's printing the whole list instead of each variable inside it. I'm using jinja2 templates.
<table>
<tr>
<th>Date:</th>
<th>Scheduled:</th>
</tr>
{% for day in working_days %}
<tr>
<td>{{working_days}}</td>
</tr>
{% endfor %}
</table>
Right now it's just displaying the whole list 7 times instead of each variable from the list in a seperate td.
['maandag 12 maart', 'dinsdag 13 maart', 'woensdag 14 maart',
'donderdag 15 maart', 'vrijdag 16 maart', 'zaterdag 17 maart', 'zondag
18 maart']

This would solve the problem:
{% for day in working_days %}
<tr>
<td>{{day}}</td> {# <- not working_days #}
</tr>
{% endfor %}

<table>
<tr>
<th>Date:</th>
<th>Scheduled:</th>
</tr>
{% for day in working_days %}
<tr>
<td>{{ day }}</td>
</tr>
{% endfor %}
replace {{day}} with {{working_days}}
Hope , It'll Help you.

You just need more for
{% for day in working_days%}
{% for d in day %}
<tr><td>{{d}</td></tr>
{% endfor %}
{% endfor %}

Related

multiple python lists to table rows in HTML

I have multiple lists created in python. I am trying to convert these lists into HTML rows.
week = ['1','2']
date = ['2022-10-01','2022-10-09']
My HTML table should look like below:
Week Date
1 2022-10-01
2 2022-10-09
what I tried so far:
{% for val in data %}
<tr>
<td>
{% for item in val.week %} {{ item }} <br> {% endfor %}
</td>
<td>
{% for item in val.date %} {{ item }} <br> {% endfor %}
</td>
</tr>
{% endfor %}
The problem with above code is every value is treated as cell instead of row and hence I am unable to apply any row related styles to the above.
Could someone help with how to convert these to rows instead of cells.
Thank you.
You can use dictionary comprehension to convert the two lists into a dictionary where the key would be the week number and the value would be the corresponding date. You can loop over the dictionary as key and value using the items attribute in the template.
# views.py
week = [1, 2]
date = ['2022-10-01','2022-10-09']
data = {week[i]: date[i] for i in range(len(week))}
return render(request, 'template.html', {'data': data})
# template.html
{% for key, value in data.items %}
<tr>
<td> {{ key }} </td>
<td> {{ value }} </td>
</tr>
{% endfor %}
try this approch...
------ views.py ------
week = ['1','2']
date = ['2022-10-01','2022-10-09']
data = tuple(zip(week,date))
------ html ------
<table style="border: 1px solid; border-collapse: collapse;">
<thead>
<th>Weeks</th>
<th>Dates</th>
</thead>
<tbody>
<tbody>
{% for i,j in data %}
<tr>
<td>{{i}}</td>
<td>{{j}}</td>
</tr>
{% endfor %}
</tbody>
</tbody>
</table>
-------- Output -------

how to print python list in html table horizontally (django)

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

How to iterate over a list value of a dictionary in django template

I have a dictionary where value are in list
{
'Fees': ['88000', '88000'],
'ROll Number': ['I0', 'I1'],
'Mark': [10, 10]
}
So I am trying to insert this data in table, SO my Django template are
<table>
<thead>
<tr>
{% for k, v in loan_bank_info.items %}
<th>{{ k }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for k, value in loan_bank_info.items %}
{% for v in value %}
<td>{{ v }}</td>
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
but in table value are printing as follow,
Fees ROll Number Mark
88000 88000 I0 I1 10 10
But what I want is -
Fees ROll Number Mark
88000 I0 10
88000 I1 10
how to iterate over list value in Django template
You can just iterate over the list. Something like below
<tr>
{% for k, value in loan_bank_info.items %}
{% for v in value %}
{% for i in v %}
<td>{{ i }}</td>
{% endfor %}
{% endfor %}
{% endfor %}
</tr>

jinja2: building table by iterating down columns instead of across rows

If I have the following lists:
headers = ['Name', 'Age']
rows = [['Johnny', 30], ['Zack', 20]]
I can easily make a table via Jinja2 (https://jsfiddle.net/equbh9du/1/):
<table class="table table-bordered table-hover">
<thead>
<tr>
{% for h in headers %}
<td>{{ h }}</td>
{% endfor %
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
I'm noticing it's much easier (and more organized) to return my data as a dictionary:
d = {'Name': ['Johnny', 'Zack'], 'Age': [30, 20]}
Is there an easy way to build the table I built above using this dict. I imagine I need to finish the iteration down each column before continuing to the next column (in the example above I finish the iteration across each row before continuing to the next row).
This is the code I have so far but I'm getting a messed up table (https://jsfiddle.net/j164fqy9/1/):
<table class="table table-bordered table-hover">
<thead>
<tr>
{% for h in d %}
<td>{{ h }}</td>
{% endfor %
</tr>
</thead>
<tbody>
{% for h, col_values in d.items() %}
{% for item in col_values %}
<tr>
<td>{{ item }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
EDIT: If HTML standard prevents iterating down columns first, then I need to construct the headers and rows from d. Is below the best way to do this?
headers = [h for h in d]
rows = [[l[i] for h, l in d.items()] for i in range(len(d['Name']))]
If you allways have the same number of items in col_values this should work fine :
{% for i in range(d['Name']|count) %}
<tr>
{% for k in d %}
<td>{{ d[k][i] }}</td>
{% endfor %}
</tr>
{% endfor %}

Django templates count total in forloop

I have the folowing code in Django templates.
{% if levels %}
{% for l in levels %}
<tr>
<td class="level">{{ l.skill }}</td>
{% for e in employees %}
{% if e.skill_level_id == l.id %}
<td class="race">{{ forloop.counter0 }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
What this gives me is something like the following (in HTML table):
level1 1 5
level2 4 2 6 3
Which means 2 employees at level1 (1 and 5) and 4 employees in level2. What I would like is the total of each level:
level1 2
level2 4
I'm not sure if I should use forloop.counter, forloop.counter0 or forloop.parentloop.counter? Or something completely different?
Rather than looping and filtering, you should use regroup to group your objects into skill levels. Something like:
{% regroup employees by skill_level as employees_by_level %}
{% for level in employees_by_level %}
<tr>
<td class="level">{{ level.grouper.skill }}</td>
<td class="count">{{ level.list|length }}</td>
{% for e in level.list %}
<td class="race">{{ forloop.counter0 }}</td>
{% endfor %}
</tr>
{% endfor %}
Note you'll need to have employees sorted by skill_id in the first place for this to work properly.

Categories

Resources