Django/DataTables - Template Loop breaks DataTable - django

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.

Related

How to ADD for loop result in templates

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

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 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 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 Template : Combine 2 loops over different lists

I'm wondering how I can combine these 2 querysets in my template in order to loop over them.
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id'))
max_download_number = Download.objects.values('pub__age_id').annotate(max_usage=Max('usage'))
context = {'requests': requests, 'max_download_number': max_download_number}
In my template :
{% for item in requests %}
{% for element in max_download_number %}
<tr>
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ element.max_usage }}</td>
<td>Something</td>
</tr>
{% endfor %}
{% endfor %}
It displays wrong loops :
Why don't you combine it in the view:
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id')).annotate(max_usage=Max('usage'))
and then in the template:
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ item.max_usage }}</td>