DTL 模板 for

代码1:for in 循环:

遍历列表

views.py文件:
from django.shortcuts import render

def index(request):
context = {
“books”:[
“三国演义”,
“红楼梦”,
“水浒传”,
“西游记”
]
}
return render(request,‘index.html’,context=context)

index.html文件:

Title
  • {% for book in books %}
  • {{ book }}
  • {% endfor %}
    </ul>
    

反向遍历index.html:

Title
  • {% for book in books reversed %} ---使用reversed字段
  • {{ book }}
  • {% endfor %}
    </ul>
    

遍历字典:

views.py代码:
from django.shortcuts import render

def index(request):
context = {
“books”:[
“三国演义”,
“红楼梦”,
“水浒传”,
“西游记”
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
}
}
return render(request,‘index.html’,context=context)
index.html代码:

Title
  • {% for book in books reversed %}
  • {{ book }}
  • {% endfor %}
    </ul>
    <ul>
        {% for key in person.values %}
            <li>{{ key }}</li>
        {% endfor %}
    
    </ul>
    

index.html文件:

Title
  • {% for book in books reversed %}
  • {{ book }}
  • {% endfor %}
    </ul>
    <ul>
        {% for key,values in person.items %}    ----遍历字典的键值对。
            <li>{{ key }}/{{ values }}</li>
        {% endfor %}
    
    </ul>
    

遍历列表当中的字典:

index.html

Title
  • {% for key,values in person.items %}
  • {{ key }}/{{ values }}
  • {% endfor %}
    </ul>
    <table>
        <thead>
            <tr>
                <td>书名</td>
                <td>作者</td>
                <td>价格</td>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
                <tr>
                    <td>{{ book.name }}</td>
                    <td>{{ book.author }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
    
        </tbody>
    </table>
    

view.py:文件:

from django.shortcuts import render

def index(request):
context = {
“books”:[
{
“name”:“三国演义”,
“author”:“罗贯中”,
“price”:100
},
{
“name”:“水浒传”,
“author”:“施耐庵”,
“price”:120
},
{
“name”:“西游记”,
“author”:“吴承恩”,
“price”:130
},
{
“name”:“红楼梦”,
“author”:“曹雪芹”,
“price”:200
}
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
}
}
return render(request,‘index.html’,context=context)

给遍历的标签添加背景效果:

Title
  • {% for key,values in person.items %}
  • {{ key }}/{{ values }}
  • {% endfor %}
    </ul>
    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>书名</td>
                <td>作者</td>
                <td>价格</td>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
                {% if forloop.first %}
                    <tr style="background: pink">
                {% elif forloop.last %}
                    <tr style="background: blue">
                 {% else %}
                    <tr>                 ------注意这里是开始的tr标签
                {% endif %}
                    <td>{{ forloop.counter }}</td>
                    <td>{{ book.name }}</td>
                    <td>{{ book.author }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
    
        </tbody>
    </table>
    

for …in…empty代码:

views.py代码

from django.shortcuts import render

def index(request):
context = {
“books”:[
{
“name”:“三国演义”,
“author”:“罗贯中”,
“price”:100
},
{
“name”:“水浒传”,
“author”:“施耐庵”,
“price”:120
},
{
“name”:“西游记”,
“author”:“吴承恩”,
“price”:130
},
{
“name”:“红楼梦”,
“author”:“曹雪芹”,
“price”:200
}
],
“person”:{
“username”:“张三”,
“age”:18,
“height”:180
},
“comments”:[ ------文章的评论
“文章不错”,
“写的好”
]
}
return render(request,‘index.html’,context=context)

index.html代码:

Title
  • {% for key,values in person.items %}
  • {{ key }}/{{ values }}
  • {% endfor %}
    </ul>
    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>书名</td>
                <td>作者</td>
                <td>价格</td>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
                {% if forloop.first %}
                    <tr style="background: pink">
                {% elif forloop.last %}
                    <tr style="background: blue">
                 {% else %}
                    <tr>
                {% endif %}
                    <td>{{ forloop.counter }}</td>
                    <td>{{ book.name }}</td>
                    <td>{{ book.author }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
    
        </tbody>
    </table>
    <ul>
        {% for comment in comments %}        -------演示遍历评论,有就打印,没有打印没有评论
            <li>{{ comment }}</li>
        {% empty %}
            <li>没有任何评论</li>
        {% endfor %}
        
    </ul>
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值