In the for tag of the Django template system, you can learn the following points: forloop. counterforloop. counter0forloop. firstforloop. lastforloop. revcounterforloop. revcounter0froloop. parentloop.
Empty
The following is an example:
First, go to our project directory and enter:
Python manage. py Shell
# Forloop. counterfrom Django. template import context, templatet = template ("" {% for item in our_list %} <p >{{ forloop. counter }}:{ {item }}</P? {% Endfor %} ") C = context ({'hour _ list': [X for X in range (10)]}) print T. render (c) # forloop. counter0from Django. template import context, templatet = template ("" {% for item in our_list %} <p >{{ forloop. counter0 }:{{ item }}</P? {% Endfor %} ") C = context ({'hour _ list': [X for X in range (10)]}) print T. render (c) # forloop. firstt = template ("{% for object in objects %} {% if forloop. first %} <li class = "first" >{% else %}< Li >{% endif %}{ {object }}</Li >{% endfor %} c = context ({'objects ': [X for X in range (10)]}) print T. render (c) # forloop. lastt = template ("{% for link in links % }{{ link }{% if not forloop. last %} | {% endif %} {% endfor %} ") C = context ({'link ': ['link' + X for X in range (10)]}) print T. render (c) # forloop. parentloopt = template ("{% for country in countries %} <Table >{% for city in country. city_list %} <tr> <TD> country # {forloop. parentloop. counter }}</TD> <TD> city # {forloop. counter }}</TD> <TD >{{ city }}</TD> </TR >{% endfor %}</table >{% endfor % }""") china = {'city _ list': ['shanghai', 'beijing']} USA = {'city _ list': ['xxxx ', 'yyy']} countries = [China, USA] C = context (countries) T. render (c)
This article is from the "Wooden blog" blog, please be sure to keep this source http://ggbond.blog.51cto.com/8886865/1432778