How to ADD for loop result in templates - django

views.html
{% for products in product %}
<tr>
<td>{{ products.quantity_deliver1 }}</td>
<td>{{ products.quantity_deliver2 }}</td>
<td>{{ Code Here }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3" class="text-center bg-warning">No Products</td>
</tr>
{% endfor %}
How do I add products.quantity_deliver1 + products.quantity_deliver2 and output the sum in the 3rd data cell.

You can use builtin django template tag add for adding two.
{{products.quantity_deliver1|add:products.quantity_deliver2}}
Ref - https://docs.djangoproject.com/en/dev/ref/templates/builtins/#add

Related

Return the total of a group with Jinja

I have an object to return by group and then display the number of grouped items. example :
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% set count = 0 %}
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{% for number in item.list %}{% set count = count + 1 %}{{ count }}{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
</table>
I also tried :
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{% for number in item.list %}{{ loop.length }}{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
</table>
But the return for a total of 3 is: "111" or "333" and i want display "3",
avez-vous une idée ?
Be careful in jinja the "+" is for concatenation not for addition. For that there is a filter |add but right now if I understand what you want you don't need that:
<table>
<thead>
<tr>
<th>Name</th>
<th>Total</th>
</tr>
</thead>
<tbody>>
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{{ item.list|length }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Would be enough
<table>
<thead>
<tr>
<th>"Name"</th>
<th>"Total"</th>
<th>"%"</th>
</tr>
</thead>
<tbody>
{% set total = data.items|length %}
{% for item in data.items|groupby(attribute="name") %}
<tr>
<td>{{ item.grouper }}</td>
<td>{{ item.list|length }}</td>
<td>{{ total*(item.list|length)*0.1 }}{{ "%" }}</td>
</tr>
{% endfor %}
</tbody>
</table>

How to print only certain cells of my table in Django templates?

I am developing an app in Django.
I have on my template (code 0):
{% for row in query_result %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
Let's suppose I want my template to print only the first and the third column of my matrix, how can I indicate to my template a specific cell?
I tryed with
{% for row in query_result %}
<tr>
<td >{{ cell[0] }}</td>
<td >{{ cell[2] }}</td>
</tr>
{% endfor %}
but it does not work.
EDIT:
As suggested, I tryed with (code 2):
{% for row in query_result %}
<tr>
<td >{{ cell.0 }}</td>
<td >{{ cell.2 }}</td>
</tr>
{% endfor %}
But this somehow erases the content of my cells, see here below.
results of Code 0:
results of code 2:
You can obtain an item at an index in Django's template engine by fetching it as you would fetch an attribute, so:
{% for row in query_result %}
<tr>
<td >{{ row.0 }}</td>
<td >{{ row.2 }}</td>
</tr>
{% endfor %}
That being said, I strongly advise to try to fix the problem upstream: instead of rendering a subset, take a look what you can do to limit the "columns" in query_result.
or you can unpack the elements and put the second item in a "throwaway" variable:
{% for cell0, __, cell2 in query_result %}
<tr>
<td >{{ cell0 }}</td>
<td >{{ cell2 }}</td>
</tr>
{% endfor %}
Use looks like {{ cell.0 }} this. The Django docs explain in the section variables and lookups.
So you would do something like:
{% for row in query_result %}
<tr>
<td >{{ cell.0 }}</td>
<td >{{ cell.2 }}</td>
</tr>
{% endfor %}

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 %}

Django/DataTables - Template Loop breaks DataTable

When placing a template loop in my table-row my DataTable breaks.
<table id="store_table" class="table-striped table-hover">
<thead class="thead-light">
<tr>
<th>Store #</th>
<th>Name</th>
<th>Phone</th>
<th>City</th>
<th>State</th>
</tr>
</thead>
<tbody>
{% for store in stores %}
<tr id="table-row">
<td>{{ store.store_number }}</td>
<td>{{ store.name }}</td>
<td>{{ store.phone }}</td>
<td>{{ store.city }}</td>
<td>{{ store.state }}</td>
{% for circuit in store.circuit_set.all %}
<td>{{ circuit.circuit_id }}</td>
{% endfor %}
<td>{{ store.postal }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Console Output:
jQuery.Deferred exception: i is undefined Ha#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:24:397
O#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:16:421
na/<#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:17:21
map/<#https://code.jquery.com/jquery-3.3.1.min.js:2:1324
map#https://code.jquery.com/jquery-3.3.1.min.js:2:3169
map#https://code.jquery.com/jquery-3.3.1.min.js:2:1292
na#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:16:497
e#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:92:431
n/<#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:93:118
each#https://code.jquery.com/jquery-3.3.1.min.js:2:2571
each#https://code.jquery.com/jquery-3.3.1.min.js:2:1238
n#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:83:194
h.fn.DataTable#http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js:165:488
#http://10.200.20.63:8080/stores/:12810:19
l#https://code.jquery.com/jquery-3.3.1.min.js:2:29373
a/</c<#https://code.jquery.com/jquery-3.3.1.min.js:2:29677
undefined
So I'm assuming this is because {% %} isn't a recognized/handleable table element.
You table break for this
{% for circuit in store.circuit_set.all %}
<td>{{ circuit.circuit_id }}</td>
{% endfor %}
If you change this to
<td>
{% for circuit in store.circuit_set.all %}
<p>{{ circuit.circuit_id }}</p>
{% endfor %}
</td>
This may works.

Django Template - Need to use filter()

I am kinda stuck.
Here is the situation. I have 2 for loops. One that loops through categories and the other one that loops through a match set. Here is the problem:
I need to get all the matches from that category...
Here is what I have:
{% for category in tournament.get_categories %}
<div class="line" style="margin-top: 25px; margin-bottom: 40px;"></div>
<div class="container">
<h3 class="margin-reset" id="{{ category.slug }}">{% trans "Matches schedule" %}<span> | {{ category.name }}</span></h3>
<table class="standard-table table">
<thead>
<tr>
<th>Match</th>
<th>Heure</th>
<th>Plateau</th>
<th>Équipe bleu</th>
<th>Équipe gris</th>
<th>Équipe noir</th>
<th>Résultat</th>
</tr>
</thead>
<tbody>
{% for match in tournament.match_set.filter(category=category) %}
<tr>
<td>{{ match.match_num }}</td>
<td>{{ match.time }}</td>
<td>{{ match.plateau }}</td>
<td>{{ match.blue_team.name }}</td>
<td>{{ match.grey_team.name }}</td>
<td>{{ match.black_team.name }}</td>
<td>{{ match.get_url_string }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
As you guys probably guessed it, I get this error:
Could not parse the remainder: '(category=category)' from 'tournament.match_set.filter(category=category)'
What can I do?
Thanks,
Ara
EDIT:
Here is the solution:
The custom tag:
#register.filter
def get_matches_by_category(value, category):
return value.match_set.filter(category=category)
And the use:
{{ tournament|get_matches_by_category:category }}
I created a custom templatetag and used that filter.
It's kinda an overkill but...oh well.
What I have done in a very similar case is pass the list of categories to the template, adding them a new attribute with the matches, like:
for category in categories:
category.matches = Match.objects.filter(tournament=tournament, category=category)
It's kinda slow, but you can work it and reduce the number of queries
I would pass the categories list to the template after this