ELIF using not in, inside a FOR

Asked

Viewed 77 times

0

I would like to know if there is any way to refine the ELIF within a FOR, so that it does not enter into it whenever the date does not exist, but if that date specifies it does not exist.

Follow the problem:

For each date meeting

{%for u in i.chamada_set.get.datas_encontros.filter|dictsort:"data_ano"%}

Log in for each completed date

{% for a in i.chamada_set.get.finalizada_set.filter %}

And checks whether:

{%if a.data_finalizada|date:"d, m, Y" == u.data_ano|date:"d, m, Y"%}

The completed date is equal to the date I meet, however, if I have 3 completed dates, only 1 would fall in this if and the other 2 in Else

So I thought about the ELIF

{%elif u.data_ano|date:"d, m, Y" not in a.data_finalizada|date:"d, m, Y" %}

But continued to enter because the other dates completed, are not equal to date.

Imagery:

elif

In green are completed dates, in Person One, has the following order of finalized date [06/11/2018, 08/11/2018, 13/11/2018]

The main idea would be to show that it did not find the date, and it works when it has only one finished (Person Two and Person Three).

Code of the TR:

{% for y in i.chamada_set.get.alunos.filter %}
    <tr>
    <td>{{y.nome_aluno}} {{y.sobrenome_aluno}} </td>
    <td>{{y.cgu_aluno}}</td>
    {% for u in i.chamada_set.get.datas_encontros.filter|dictsort:"data_ano"%}
        {% for a in i.chamada_set.get.finalizada_set.filter %}
            {%if a.data_finalizada|date:"d, m, Y" == u.data_ano|date:"d, m, Y" and y.id == a.aluno_finalizado.id %}
                <td style="color:green">
                    <i class="">encontrou </i>
                    <i class="">{{u.data_ano|date:"d, m, Y"}}</i>
                </td>
            {%elif u.data_ano|date:"d, m, Y" not in a.data_finalizada|date:"d, m, Y" and y.id == a.aluno_finalizado.id  %}
                <td  style="color:tomato">
                    <i class="">não é a data: </i>
                    <i class="">{{u.data_ano|date:"d, m, Y"}}</i>
                </td>
            {%endif%}
        {%endfor%}
    {%endfor%}
    </tr>
{%endfor%}

2 answers

1

I removed this part of the Jinja2 documentation: Template Designer Documentation

Unlike in Python, it’s not possible to break or continue in a loop. You can, However, filter the Sequence During iteration, which Allows you to Skip items. The following example skips all the users which are Hidden:

{% for user in users if not user.hidden %}
    <li>{{ user.username|e }}</li>
{% endfor %}

One tip that can help is to use the for index as a counter to help you escape in if.

0

I found this other information that can help a lot. For has no break but if yes. give a look if it helps you

Link to the document

Loop Controls

If the application Enables the Loop Controls, it’s possible to use break and continue in loops. When break is reached, the loop is terminated; if continue is reached, the Processing is stopped and continues with the next iteration.

Here’s a loop that skips Every Second item:

{% for user in users %}
    {%- if loop.index is even %}{% continue %}{% endif %}
    ... {% endfor %} 

Likewise, a loop that stops Processing after the 10th iteration:

{% for user in users %}
    {%- if loop.index >= 10 %}{% break %}{% endif %} 
{%- endfor %}

Note that loop.index Starts with 1, and loop.index0 Starts with 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.