Django Template : Combine 2 loops over different lists - django

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>

Related

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>

Django Template IF inside Listing FOR

I am tryin use an object from a listing in an IF structure inside the FOR LOOP, but when I am trying to compare the object whit a String (That is 'TRUE'), I can not go inside the True case lines of the IF structure.
Example:
When equipo.Department = "Equipo", i don know why the IF ({% if equipo.Department == 'Equipo' %}) is not working.
Code:
{% autoescape off %}
{% if equipos %}
{% for equipo in equipos %}
<tr></tr>
<td>{% if equipo.Department == 'Equipo' %}
E
{% else %}{{ equipo.Department }}{% endif %}-{{ equipo.Equipment_id }}</td>
<td>{{ equipo.Name }}</td>
<td>{{ equipo.Description }}</td>
<td>{{ equipo.SerialNo }}</td>
<td>{{ equipo.Vendor }}</td>
<td>{{ equipo.Tag }}</td>
<td>{{ equipo.OutOfService }}</td>
<td>{{ equipo.Location }}</td>
<td>{{ equipo.Plan }}</td>
<td>{{ equipo.ManualPath }}</td>
<td>{{ equipo.ImagePath }}</td>
</tr>
{% endfor %}
{% else %}
<h1>No existen registros</h1>
{% endif %}
{% endautoescape %}
A Department object with 'Equipo' as name is not the same as the string 'Equipo', so the check itself is false. If you later render {{ equipo.Department }}, then Django will call the str(..) method on that result, and thus it will indeed render the name of the department.
You thus should check the equivalence with:
{% if equipo.Department.name == 'Equipo' %}
<!-- … -->
{% endif %}
That being said, instead of filtering in the template, you better should filter at the database end, so usually it is better to try to avoid retrieving equipo that is not of the required department.
You can filter for example with:
SomeModel.objects.filter(Department__name='Equipo')
Note: Django has a {% for … %}…{% empty %} template tag [Django-doc] that
can be used to render a message if the collection you iterate over is empty.

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