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 -------
Related
I have an xml object that I am looping to print in template. It is detailing of items with their tax rate. so after printing all items I need sum of those tax rate values. I am trying to sum up that value in template. I have created filter named updatevariable, it is working and doing sum up but issue is due to loose of old sum it just returns latest value rather than whole sum up. SO I need a way to reassign TotalTax with sum up value from filter.
this is example of output needed:
Tax-1 16
Tax-2 18
Tax-3 2
TotalTax = 36
{% with TotalTax=0 %}
{% if taxRateDetailing %}
{% for item in taxRateDetailing%}
<tr>
<td>
{{item.1.text}}
</td>
<td>
{{item.4.text}}
{{TotalTax|updatevariable:item.4.text}}
</td>
</tr>
{% endfor %}
<tr>
<td>
Total Tax
</td>
<td>
{{TotalTax}}
</td>
</tr>
{% else %}
<tr>
<td>
Tax
</td>
<td>
{{defaultSalesTaxRate}}
</td>
</tr>
{% endif %}
{% endwith %}
Filter function
def updatevariable(value, arg):
TotalTax = value + float(arg)
return TotalTax
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>
I am creating a table in html, but if the value that will be outputted is equal to the value above I want to merge the cells. I am sure that all the variables work as they are correct later. However, when I use them in the if loop I get an error
<table>
<tr>
<th>id</th>
<th>peptide_id</th>
<th>protein_id</th>
<th>group_id</th>
<th>search_id</th>
<th>peptide_parsimony</th>
</tr>
{% for elem in elem_list %}
<tr>
{% for sub_elem in elem %}
elem.2 =
{% if {{ elem.2 }} == {{sub_elem}} %}
<td> </td>
{% else %}
<td onclick="location.href='/protein/proteinseq/{{ elem.1}}/{{ elem.2 }}/{{ elem.4 }}/'" style = " text-decoration: underline; cursor: pointer" >{{ sub_elem }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
This gives me the error :
Could not parse the remainder: '{{' from '{{'
Don't use tag or variable brackets when you are already within a tag.
{% if elem.2 == sub_elem %}
I made this table but I can't figure out how to do the numbering. I want a top 10 list and I get all the data from a database. I essentially want to make it show '1' and the next '2' and the next '3' and so forth. How would I able to do this?
https://imgur.com/a/9Isf941
def home(request):
straatData = data.objects.all()[:10]
count = data.objects.count()
numbers = []
for dataCount in range(1, count + 1):
numbers.append(dataCount)
context = {
'data': straatData,
'count': numbers,
}
return render(request, 'home.html', context)
I need to get the top numbers to show 1,2,3,4... as many as the results. But I don't know how to do it
You don't need to set a context variable for that. You can use forloop.counter in your template code.
{% for item in data %}
<tr>
<td> {{ foorlop.counter }} </td>
<td> {{ item }} </td>
</tr>
{% endfor %}
If you want to start counting at 0, use forloop.counter0
Perhaps you should be using jinja to render the table in a loop?
In your html:
{% for num in count %}
<tr>
<td> {{ num }} </td>
<td> {{ data[num][0] }} </td>
<td> {{ data[num][1] }} </td>
<!-- Im not sure how your data is formatted! -->
</tr>
{% endfor %}
Without seeing your html it's hard to know how you're currently rendering the table.
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 %}