Home > Article > Web Front-end > How to control the loop variable
This look
Because I am responsible for the front-end, I am not very familiar with views. I want to control them directly in the template. After searching, I found that there is no need to directly use multiplication, division or mod calculation, which is embarrassing.
Reference article: http://blog.csdn.net/rain_qingtian/article/details/41076151
Easy to know, Django template addition:
{{ value|add: 10 }}
|
value=5, then 15 is returned. Django template subtraction:
{{value|add: - 10 }}
|
value=5, then -5 is returned. This is easier to understand. Subtraction is to add a negative number. Django template multiplication:
{ % widthratio 5 1 100 % }
|
The above code means: 5/1 *100, returns 500, widthratio requires three parameters, it will use parameter 1/parameter 2 * parameter 3, so if you want to perform multiplication, just set parameter 2 = 1 to perform Django template division
{ % widthratio 5 100 1 % }
|
The above code means: 5/100*1, returns 0.05, just set the third parameter to 1.
But these methods are very troublesome to use for remainder division.
Solution: divisibleby tags!
Use Django’s divisibleby tag to implement it, as follows:
The meaning of the{% for each in somelist %}
{% if forloop.counter|divisibleby:2 %}
{% else %}
{% endif %}
{% endfor %}
divisibleby tag is to use the following parameters to remove it, and all the exceptions are True, otherwise it is False.
So, my code changed to:
{{ value }} {{ value }} {% endif %}
{% for key,value in formextenddetail %}
{% if forloop.counter|divisibleby:'2' %}
< td style="width: 50%" >
{% else %}
{% endfor %}
This solution can also be used for line breaks and style changes, etc.!