Django learning template System

Source: Internet
Author: User

 
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.