Django templates count total in forloop - python

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.

Related

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>

Python list(s) into html 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 %}

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>

How to check if that which is iterated upon is the last key/value in a dict? (probably needs to be done in django template)

The desired outcome is a navigation that look like so:
A | B | C | D | E | ... | X | Y | Z
Note that both A and Z do not have the pipe on the outsides.
This is the template I have currently.
<section id="partners_nav">
<div class="row">
<table align="center" cellpadding="4px">
<tr>
{% for key, value in index.items %}
{% if key in index|last %}
{% if value == None %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% else %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% endif %}
{% else %}
{% if value == None %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% else %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
</table>
</div>
The line {% if key in index|last %} is where i'm attempting to check if it is the last item in the iteration.
This does not produce any errors.Yet does not remove the pipe to the right of the letter Z.
Note:
Inside of index is an ordered dictionary which has a key for every letter in the alphabet. And some of these keys have values that are also a letter (the same letter)... This is to use the A | B | C as a jump to navigation at the top of the page. The rest of the keys have a value of None... So the letter still displays at the top of the page but it is not clickable
You might want to check forloop.last template variable. It returns True if it is the last time through the loop:
{% for key, value in index.items %}
...
{% if value %}
<td>{{ key }}</td>
{% else %}
<td>{{ key }}</td>
{% endif %}
{% if not forloop.last %}
<td id="partners_nav_bracket">|</td>
{% endif %}
...
{% endfor %}
You can make use of forloop.first or forloop.last.
<div class="row">
<table align="center" cellpadding="4px">
<tr>
{% for key, value in index.items %}
{% if value == None %}
<td>{{ key }}</td>
{% else %}
<td>{{ key }}</td>
{% endif %}
{% if not forloop.last %}
<td id="partners_nav_bracket">|</td>
{% endif %}
{% endfor %}
</tr>
</table>
</div>
I also cleaned up your markup, because the contents of your if and else were the same.

How to go through two loops in django templates

Can anyone tell me what am I doing wrong in this code:
{% for dayName in data %}
<tr>
<td>{{ dayName }}</td>
{% for value in data.dayName %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
data is an object containing arrays, for an instance:
data['Sunday'] = [1 ,2 ,3]
And all what I want to do is create two loops through that object.
I will be thankful for each form of help,
Thanks in advance
dayName is a variable not the key itself. data.dayName is interpreted as data['dayName'], that's why you're not getting the right results.
Instead, you can do:
{% for dayName, vals in data.items %}
<tr>
<td>{{ dayName }}</td>
{% for value in vals %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}

Categories

Resources