Home  >  Article  >  Web Front-end  >  How to control the loop variable

in Django template if there are two in one row

How to control the loop variable

in Django template if there are two in one row
WBOY
WBOYOriginal
2016-08-04 08:53:141921browse

2016-8-3 Wednesday

Problems encountered when working on projects:

Each div is output by the loop variable:

{% for key,value in formextenddetail %}


{{ key }}< ;/div>



{% endfor %}

But I want two divs in one row, use

...

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:

{% for each in somelist %}

{% if forloop.counter|divisibleby:2 %}

{% else %}

{% endif %}

{% endfor %}

The meaning of the

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:


{% for key,value in formextenddetail %}
{% if forloop.counter|divisibleby:'2' %}
< td style="width: 50%" >

{{ key } }

{{ value }}





{% else %}

{% endif %}
{% endfor %}



{{ key }}

{{ value }}



This solution can also be used for line breaks and style changes, etc.!

                                                                        

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:css3 filtering effectNext article:css3 filtering effect