Django Template IF inside Listing FOR - django

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.

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>

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>

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>

django display in html

I have two classes in my models:
class FicheFormation(models.Model):
intitule=models.TextField(blank=False,null=False)
duree=models.IntegerField(blank=False,null=False)
objective=models.TextField(blank=False,null=False)
niveau=models.TextField(blank=False,null=False)
prix=models.IntegerField(blank=False,null=False)
comptecf=models.ForeignKey('CompteRF',related_name='fichecompte',null=False)
unite= models.ManyToManyField('UniteFormation', related_name='unitefiche')
and
class UniteFormation(models.Model):
nomUe=models.TextField(blank=False,null=False)
acronyme=models.TextField(blank=False,null=False)
commentaire=models.TextField(blank=True,null=True)
and I want to display the list of formations of a comptecf,
so i write in my view this method:
def getFormation(request):
# user = request.user
# if user and user.is_active:
# u=user.username
c=CompteRF.objects.all().filter(username='Me')
queryset=FicheFormation.objects.all().filter(comptecf=c)
return render_to_response('formation.html', {'formations':queryset},
context_instance=RequestContext(request))
and in my formations.html:
% for car in formations %}
<tr>
<td>{{ car.intitule}}</td>
<td>{{ car.objective }}</td>
<td>{{ car.unite }}</td>
</tr>
{% endfor %}
update:
{% for car in formations %}
<tr>
<td>{{ car.intitule}}</td>
<td>{{ car.objective }}</td>
% for car in formations %}
<td>{{ car.unite.nomUe }}</td>
{% endfor %}
</tr>
{% endfor %}
it works for =car.intitule and car.objective but for car.unite it displays None, I haven't understand why because normaly it displays unite 'Sarra', in the console I need to do : c=unite.all() and then c[0].nomUe and it works and affich 'Sara' but in my view or html when i try to iterate on car.unite I have en error message !
To loop through the related unite objects, you need to loop through car.unite.all(). In the template, you drop the parentheses, so you do something like:
<td>{% for unite in car.unite.all %}{{ unite.nomUe }}{% endfor %}</td>

Blocktrans correctly written in django template

I want my django template variables were possible to translate. I used for this purpose {% blocktrans %}.
Is this code is correct and can be optimized. I mean to write better? The loop or something like that.
{% for obj in objects %}
{% blocktrans with obj.user as user and obj.country as country and obj.b_day as b_day %}
<tr>
<td>{{ user }}</td>
<td>{{ country }}</td>
<td>{{ b_day }}</td>
</tr>
{% endblocktrans %}
{% endfor %}
Hm, I suspect your code don't work properly. Anyway the simple {% trans %} tag looks much better:
{% for obj in objects %}
<tr>
<td>{% trans obj.user %}</td>
<td>{% trans obj.country %}</td>
<td>{% trans obj.b_day %}</td>
</tr>
{% endfor %}