double curly braces in quote mark - Django - django

Could some one tell me the full picture of the use of the quote mark and the double curly braces?
<a href="{{ url_for('delete_event', id=event.id) }}" class="text-danger">
The full code is as below. Is it Django synatx {{ ... }} embeded?
{% for event in events %}
<tr>
<td>{{ event.event }}</td>
<td>{{ event.start_time }}</td>
<td>{{ event.end_time }}</td>
<td>{{ event.position }}</td>
<td>Delete</td>
</tr>
{% endfor %}

You need to correct url in template like this django does not support jinja tag, but django have a built in template language is called DTL (Django Template Language) so you need to correct in your code url pattern like this
{% for event in events %}
<tr>
<td>{{ event.event }}</td>
<td>{{ event.start_time }}</td>
<td>{{ event.end_time }}</td>
<td>{{ event.position }}</td>
<td>Delete</td>
</tr>
{% endfor %}

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

Show string if value is None in template

If a value is None I would like to show a - rather than rendering None in the table.
However I get the following error:
Could not parse the remainder: ' or '-'' from 'employee.full_name or '-''
I'd like to avoid doing a ton of if else statements if possible.
Here's my code:
{% for employee in employees %}
<tr>
<td>{{employee.full_name or '-'}}</td>
<td>{{employee.position or '-'}}}</td>
<td>{{employee.dob or '-'}}}</td>
<td>{{employee.phone or '-'}}}</td>
<td>{{employee.email or '-'}}}</td>
<td>{{employee.address or '-'}}}</td>
<td>{{employee.joined or '-'}}}</td>
</tr>
{% endfor %}
The following Python code works as expected:
dog = None
print(dog or '-')
You can work with the |default_if_none template filter [Django-doc]:
{% for employee in employees %}
<tr>
<td>{{ employee.full_name|default_if_none:'-' }}</td>
<td>{{ employee.position|default_if_none:'-' }}</td>
<td>{{ employee.dob|default_if_none:'-' }}</td>
<td>{{ employee.phone|default_if_none:'-' }}</td>
<td>{{ employee.email|default_if_none:'-' }}</td>
<td>{{ employee.address|default_if_none:'-' }}</td>
<td>{{ employee.joined|default_if_none:'-' }}</td>
</tr>
{% endfor %}

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