Django pattern loop for alternating lines - no loop

This may not be a question, but how do you use Django functionality {% cycle %}or something similar when you're not in a loop? In particular, I have an HTML table that I write manually, as this is not what you need to do in the loop. I want the lines to alternate, for example:

   <tr class="{% cycle 'even' 'odd'%}"></tr>
   <tr class="{% cycle 'even' 'odd'%}"></tr>
   <tr class="{% cycle 'even' 'odd'%}"></tr>

But I do not use a loop, so this always results in even. I do not need a situation where I want to insert one line later, and then I have to change the classes of all the lines under it manually. Am I just a little girl? How would you switch without being in a loop?

+5
source share
1 answer

, cycle :

- . {% cycle%} , "", :

{% cycle 'row1' 'row2' as rowcolors %}

, , . , , . , :

<tr>
    <td class="{% cycle 'row1' 'row2' as rowcolors %}">...</td>
    <td class="{{ rowcolors }}">...</td>
</tr>
<tr>
    <td class="{% cycle rowcolors %}">...</td>
    <td class="{{ rowcolors }}">...</td>
</tr>

:

<tr>
    <td class="row1">...</td>
    <td class="row1">...</td>
</tr>
<tr>
    <td class="row2">...</td>
    <td class="row2">...</td>
</tr>

, , {% cycle name %} .

+10

All Articles