django template view nested list - django

i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}

Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>

{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

Related

How to index in Django Templates

Im trying to index a 2D list, whenever I access members.0, for example, the positioning works just fine. However I need this to change with the loop so I made a variable, but, if I was to do members.counter this wouldn't do anything.
Is there another way of doing this or is it not possible?
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members.counter }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>
You can use the square brackets [] if you need to access a list or dict at a specific position:
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members[counter] }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>

First row with two items second one and next with three

I have model (Event) and I want to have template with two ways to display items.
First row have to include two items, with special styling
Second one and next have to include three, with special styling
How can I do this with loop?
you can do like below
views.py
def view(request):
events = Event.objects.all()
l = []
for i in range(0,len(events), 5):
l.append((events[i:i+2], events[i+2:i+5]))
return render(request, "template.html", {"events": l})
template.html
{% for two_items, three_items in events %}
<tr class="class1">
{% for item in two_items %}
<td> {{ item }}</td>
{% endfor %}
<tr>
<tr class="class2">
{% for item in three_items %}
<td> {{ item }}</td>
{% endfor %}
<tr>
{% endfor %}
Combination of cycle and forloop tags will give you desired output:
For example:
{% for item in items %}
{% if forloop.counter < 3 %}
{% if forloop.first %}
<tr class="A">
{% endif %}
<td>{{ item }}</td>
{% endif %}
{% if forloop.counter == 3 %}
</tr>
{% endif %}
{% if forloop.counter >= 3 %}
{% cycle "<tr class='B'>" "" "" %}
<td>{{ item }}</td>
{% cycle "" "" "</tr>" %}
{% endif %}
{% endfor %}

Django template - How to match model datefield with a date in date generator?

In my models.py Task can have multiple Employer so I use ManyToManyField
In my views.py I got a date generator - it return range of 2 weeks from current day.
So here is my template
<table>
<thead>
<tr>
<th>{% trans 'employer_id' %}</th>
{% for i in date_range %}
<th>{{ i }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for employer in employers %}
<tr>
<td>{{ employer.employer_id }}</td>
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
What I need is when I create a Task and assign it to employers, it should show me in the html table a start_date of Task of each employers for specific days.
So far I got this with codes above:
^date of tasks of these employers are not matching with days in the table header.
Instead of
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
you need to iterate over the dates again, since you're not creating <td>'s for each date.
{% for i in date_range %}
{% for t in employer.task_set.all %}
{% if t.start_date == i %}
<td>{{ t }}</td>
{% else %}
<td>-</td>
{% endif %}
{% empty %}
<td>0</td>
{% endfor %}
{% endfor %}

How to set one counter for two forloops?

I have table:
{% for item in items %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item.field }}</td>
</tr>
{% for child in item.childs.all %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ child.field }}</td>
</tr>
{% endfor %}
{% endfor %}
But the second forloop.counter counts inside the second loop from 1. I want to get only one counter for all rows. How to do it?
You could write a custom template tag to calculate the counter (assuming that each row has an equal number of columns):
#register.simple_tag
def abs_counter(row, col, col_total)
return return ((row - 1) * col_total) + col
and
{% for item in items %}
...
{% for child in items.children.all %}
{% abs_counter forloop.parentloop.counter forloop.counter items.children.all|length %}
{% endfor %}
{% endfor %}

Blocktrans correctly written in django template

I want my django template variables were possible to translate. I used for this purpose {% blocktrans %}.
Is this code is correct and can be optimized. I mean to write better? The loop or something like that.
{% for obj in objects %}
{% blocktrans with obj.user as user and obj.country as country and obj.b_day as b_day %}
<tr>
<td>{{ user }}</td>
<td>{{ country }}</td>
<td>{{ b_day }}</td>
</tr>
{% endblocktrans %}
{% endfor %}
Hm, I suspect your code don't work properly. Anyway the simple {% trans %} tag looks much better:
{% for obj in objects %}
<tr>
<td>{% trans obj.user %}</td>
<td>{% trans obj.country %}</td>
<td>{% trans obj.b_day %}</td>
</tr>
{% endfor %}