grammar:
{% tags %}
{% For%} tags:
<ul> {% for name in name_list %} <li>counter:{{ forloop.counter }}--{{ name }}</li> {% endfor %} </ul>
{% For%} forloop tag has the default dictionary objects corresponding to the key:
{{forloop.counter}} of the current cycle number (from 1)
{} {} forloop.counter0 current cycle number (starting with 0)
{} {} forloop.revcounter current cycle number (1 to descending end)
{ {forloop.revcounter0}} of the current cycle number (0 to descending end)
{} {} forloop.first determines whether the current cycle is the first return True or False
{} {} forloop.last determines whether the current cycle is the last time True else return False
{} {} forloop.parentloop while inside the outer loop of the present cycle
view.py Code
DEF tags_test (Request): NAME_LIST = [ [ ' Wang 1 ' , ' Mike 1 ' , ' small week ' , ' Liu 1 ' ], [ ' Wang 2 ' , ' Li 2 ' , ' small Week 2 ' , ' Liu 2 ' ], [ ' Wang 3 ' , ' Li 3 ' , ' small weeks 3 ' , 'Liu 3 '], [ ' Wang 4 ' , ' Li 4 ' , ' small weeks 4 ' , ' Liu 4 ' ], ] return the render (Request, ' tags_test.html ' , { ' NAME_LIST ' : NAME_LIST})
html code
< Table border = ". 1" > < tbody > {%}% for NAME_LIST in Row < TR > {%}% for Row name in
{# parent when the sub-loop and the number of cycles can be divided by 2 text color is red # } {IF forloop.counter% | divisibleby: '2' and forloop.parentloop.counter | divisibleby: '2'}% < TD style = "Color: Red" > {{name}} </ TD > {the else%% } < TD > {{name}} </ TD > {% endif %} {% endfor %} </tr> {% endfor %} </tbody> </table>
effect:
for--empty:
< UL > {%} for obj in obj_list% < Li > {} {} obj.name </ Li > {%}% empty currently no name object obj {%} endfor% </ UL >
{% If%} tags:
if--elif--else
if--else