PyCharm says Expected }} after |default in a template - django

<div class="person_roles_div">
<table>
<thead></thead>
<tbody>
{% for position in person.positions %}
<tr>
<td>{{ position.provider|default:"" }} {{ position.role }}</td>
<td>{{ position.facility|default:"" }}</td>
<td>{{ position.startdate|default:"" }}</td>
<td>{{ position.enddate|default:"" }}</td>
<td>
<button id="remove_position_sk_{{ position.position_sk }}" class="position_remove_button">Remove</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
In the first four td tags that output a context variable and specify a default, PyCharm is highlighting the : and blank space right before and after each ""
The hovertip message is (( or {% expected. The page runs fine.
Any idea what setting I should looking at?

it's "Python template languages". See the screenshot:
Make sure it's set to "Django"

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

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

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