Jinja change variable value in IF condition - flask

I need to create a table one row and one row
so I wrote following code in template file ;
{% for dict_item in sonuc %}
{% if dict_item.status ==0 %}
{% if count == 1 %}
<tr>
{% set count = 0 %}
{% elif count == 0 %}
<tr class="alt">
{% set count = 1 %}
{% endif %}
<td>{{ dict_item.zaman }}</td><td>{{ dict_item.saat }}</td><td>{{ dict_item.kad }}</td><td>{{ dict_item.mak }}</td><td>{{ dict_item.uyg }}</td> </tr>
{% elif dict_item.status ==1 %}
{% if count == 1 %}
<tr>
{% set count = 0 %}
{% elif count == 0 %}
<tr class="alt">
{% set count = 1 %}
{% endif %}
<td><b>{{ dict_item.zaman }}</b></td><td><b>{{ dict_item.saat }}</b></td><td><b>{{ dict_item.kad }}</b></td><td><b>{{ dict_item.mak }}</b></td><td><font color="red"><b>{{ dict_item.uyg }}</b></font></td> </tr>
{% endif %}
{% endfor %}
but not change count value so all rows created with
<tr class="alt"> tag
as I think I 'm choose the wrong method for solution
I don't understand why I can't change the value of the" content " variable in if condition.

The Jinja2 cycle method is what you're looking for. There's an example in the docs. Note that one of the values in the cycle arguments can be an empty string.

Related

Jinja2 setting and using variables

I have the following
<thead>
<tr>
<th>Name</th>
{% if datas|length > 0 %}
{% set longest = [] %}
{% for data in datas %}
{% if data['numbers']|length > longest|length %}
{% set longest = data['numbers'] %}
{% endif %}
{% endfor %}
{% for i in longest %}
<th></th>
{% endfor %}
{% endif %}
</tr>
</thead>
I am trying to make enough headers to accommodate the longest list of numbers in the datas dictionary.
Can anyone see what I am doing wrong?
Chris

how to use for loop in html template

i try to use for loop in html template to make a table but the probleme he add always column outside the table like in this pic
i dont know why he continue directly out the table(if ther is one object only it's ok but when ther is more than 1 object in the ligne then this happned)
what can i do ?
thanks
html templates
<tr class="col-2">
<th>JOUR</th>
<th>8-10</th>
<th>10-12</th>
<th>12-14</th>
<th>14-16</th>
<th>16-18</th>
</tr>
<tr class="col-2">
<td>SAMEDI</td>
{% for ins in ob %}
{% if ins.jour = 'S' %}
{% if ins.heur = '1' and ins.jour = 'S' %} <td>{{ins}}</td> {% else %} <td> </td> {% endif %}
{% if ins.heur = '2' and ins.jour = 'S' %} <td>{{ins}}</td>{% else %} <td> </td> {% endif %}
{% if ins.heur = '3' and ins.jour = 'S' %} <td>{{ins}}</td>{% else %} <td> </td> {% endif %}
{% if ins.heur = '4' and ins.jour = 'S' %} <td>{{ins}}</td>{% else %} <td> </td> {% endif %}
{% if ins.heur = '5' and ins.jour = 'S' %} <td>{{ins}}</td>{% else %} <td> </td> {% endif %}
{% endif %}
{% endfor %}
{% if a != 2 %}
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
{% endif %}
</tr>
views.py
def tempss(request):
ob=temps.objects.all()
a=1
b=1
c=1
d=1
e=1
f=1
for ins in ob:
if ins.jour=='S':
a=2
elif ins.jour=='D':
b=2
model.py
class temps(models.Model):
JOUR_CHOICES = (
('S', 'Samedi'),
('D', 'Dimanche'),
('L', 'Lundi'),
('M', 'Mardi'),
('R', 'Mercredi'),
('J', 'Jeudi'),
('V', 'Vendredi'),
)
HEUR_CHOICES = (
('1', '8-10'),
('2', '10-12'),
('3', '12-14'),
('4', '14-16'),
('5', '16-18'),
)
jour = models.CharField(max_length=1, choices=JOUR_CHOICES)
heur = models.CharField(max_length=1,choices=HEUR_CHOICES)
salle=models.ForeignKey(salle,on_delete=models.CASCADE)
groupe=models.ForeignKey(groupe,on_delete=models.CASCADE,limit_choices_to={'any_field':False},)
Your loop
{% for ins in ob %}
is going to render an empty table data tag for every object where it does not fulfill the conditions you define.
This part is redundant since you already encapsulate with the same condition:
and ins.jour = 'S' %}
However, the AND makes more sense when you want to create the table (which has a fixed number of rows and cells), and put the conditions into the cells instead - I am sure there is a more elegant solution but given view and model, that's what I came up with:
<td>{% for ins in obj %}{% if ins.jour = 'S' and ins.heur = '1' %}{{ins}}
{% endif %}{% endfor %}</td>
Repeat for the rest of the timeslots accordingly.
Maybe consider constructing the table values in the view instead of the template.

Django Diff between Dates

I have a project in django and i am trying to render html to pdf.
I'm trying to build a table, and i have two varaibles date1 and date2 and i need to do a Diff between date1 and date2.
If the result is more than 20 woriking days show 1 if not show 0
MY HTML
{% for item in obj %}
<tr>
<td>
{% if item.date1 - item.date2 > 20 %}
1
{% else %}
0
{% endif %}
</td>
</tr>
{% endfor %}
You can add a method in the model to calculate difference, then use it in the templates/pdf. For example:
class SomeView(models.Model):
# .. fields
def date_diff(self):
diff = self.date1 - self.date2 # returns time delta object
return abs(diff.days)
And use it in template:
{% for item in obj %}
<tr>
<td>
{% if item.date_diff > 20 %}
1
{% else %}
0
{% endif %}
</td>
</tr>
{% endfor %}
you should perform the calculation in the backend and send number of days value while rendering the template
difference=item.date1-item.date2
days=difference.days
and in template
{% if days > 20 %}
1
{% else %}
0
{% endif %}

how to show the rank based on a field value in django Templates?

I have a list of details which contain total score. i want to show the rank based on the total score. if the scores are equal the rank must be same. how to do it?
thanks in advance.
{% for rank in ranking %}
{% with forloop.counter as count %}
<tr>
<td>{% if rank.name %} {{rank.name}} {% endif %}</td>
<td>{{count}}</td>
<td>{% if rank.total_score %}{{rank.total_score}}%{% else %} {% trans '0%' %}{% endif %}</td>
</tr>
{% endwith %}
{% endfor %}

First row with two items second one and next with three

I have model (Event) and I want to have template with two ways to display items.
First row have to include two items, with special styling
Second one and next have to include three, with special styling
How can I do this with loop?
you can do like below
views.py
def view(request):
events = Event.objects.all()
l = []
for i in range(0,len(events), 5):
l.append((events[i:i+2], events[i+2:i+5]))
return render(request, "template.html", {"events": l})
template.html
{% for two_items, three_items in events %}
<tr class="class1">
{% for item in two_items %}
<td> {{ item }}</td>
{% endfor %}
<tr>
<tr class="class2">
{% for item in three_items %}
<td> {{ item }}</td>
{% endfor %}
<tr>
{% endfor %}
Combination of cycle and forloop tags will give you desired output:
For example:
{% for item in items %}
{% if forloop.counter < 3 %}
{% if forloop.first %}
<tr class="A">
{% endif %}
<td>{{ item }}</td>
{% endif %}
{% if forloop.counter == 3 %}
</tr>
{% endif %}
{% if forloop.counter >= 3 %}
{% cycle "<tr class='B'>" "" "" %}
<td>{{ item }}</td>
{% cycle "" "" "</tr>" %}
{% endif %}
{% endfor %}