how to display grouped items in django templates? - django

I have a django template below:
{% regroup budget.productitem_set.all by group_id as grouped_product_list %}
{% for entry in grouped_product_list %}
{% for item in entry.list %}
<tbody>
<tr>
<td>{{ item.description }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.group_id }}</td>
</tr>
</tbody>
{% endfor %}
{% endfor %}
What ends up happening:
ID QUANTITY GROUP_ID
test123 23 1
test123 24 1
What output I would like
ID QUANTITY GROUP_ID
test123 23 1
24

Your also Looping description and group_Id as well using the count item
Try
{% regroup budget.productitem_set.all by group_id as grouped_product_list %}
{% for entry in grouped_product_list %}
<tbody>
<tr>
<td>{{ entry.description }}</td>
{% for item in entry.list %}
<td>{{ item.quantity }}</td>
{% endfor %}
<td>{{ entry.group_id }}</td>
</tr>
</tbody>
{% endfor %}

Related

How can i regroup some columns in django?

Here is my view :
clients = models.Client.objects.all()
sellin = models.Sellinvoice.objects.filter(client_id__in = clients)
It is my template:
<table>
<tr>
<th>ClientName</th>
<th>Price</th>
</tr>
{% regroup sellin by client.name as list %}
{% for cl in list %}
<tr>
<td>{{ cl.grouper }}</td>
<td>{{ cl.client.price }}</td>
</tr>
{% endfor %}
</table>
Result is:
ClientName
Price
(if user have two invoice this column added automatically)
john doe
30000
30000 (problem)
maria
12000
david
43000
Some user have two invoice and I can regroup ClientName but how can regroup Prices?
I resolved it by adding new loop and forloop.first element to if statement:
<table>
<tr>
<th>ClientName</th>
<th>Price</th>
</tr>
{% regroup sellin by client.name as list %}
{% for cl in list %}
<tr>
<td>{{ cl.grouper }}</td>
{% for obj in cl.list %}
{% if forloop.first %}
<td>{{ obj.client.price }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
Now result is :
ClientName
Price
john doe
30000
maria
12000
david
43000

how to get random 1/2 data from database in jinj2 template in Django?

<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
here we will get all the data from fields . but i want to only get randomly 1/2 (i can specified how many) data in jinja2 template from backend ?
How to do this ?
<table>
{% for field in fields %}
{% if forloop.counter < x %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
just put your desired number in x,you are good to go.
Try this. You can add this logic to the frontend. This way it will display records with even ID. and those will 1/2 as well.
<table>
{% for field in fields %}
<tr>
{% if field.id%2 == 0 %}
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
{% endif %}
</tr>
{% endfor %}
</table>

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>

How to add two for loops in a single table body in django template

<thead>
<th> username </th>
<th>place</th>
</thead>
{% for i, j in user_group1, user_group2 %}
<tbody>
{% if i %}
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
{% else %}
<td>{{ j.username }}</td>
<td>{{ j.place }}</td>
{% endif %}
</tbody>
{% endfor %}
I want to use two for loops in a sinle table body. First i need to start the first one and after that i need to start the next one. how can i do this
If you're using Jinja2, you can join the two lists into one with the + operator:
{% for i in user_group1|list + user_group2|list %}
<tbody>
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
</tbody>
{% 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 %}