zoukankan      html  css  js  c++  java
  • Django DTL模板语法中的循环

     1 from  django.shortcuts import render
     2 
     3 def index(request):
     4     context={
     5         'books':[
     6             '5年高考3年模拟',
     7             '家猪养殖与配种',
     8             'Python 3 面向对象编程',
     9             'MySQL数据库从删库到跑路'
    10         ],
    11         'person':{
    12             'username':'randomlee',
    13             'age':'25',
    14             'height':'180'
    15         },
    16         'book2s':[
    17             {
    18                 'name':'5年高考3年模拟',
    19                 'author':'黄冈中学',
    20                 'price':'32'
    21             },
    22             {
    23                 'name':'家猪养殖与配种',
    24                 'author':'不知道',
    25                 'price':'40'
    26             },{
    27                 'name':'Python 3 面向对象编程',
    28                 'author':'a',
    29                 'price':'22'
    30             },{
    31                 'name':'MySQL数据库从删库到跑路',
    32                 'author':'abc',
    33                 'price':'33'
    34             }
    35         ],
    36         'comments':[
    37             ' 文章的评论内容'
    38         ]
    39     }
    40     return render(request,'index.html',context)
    views.py
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <style>
     7     body {
     8         text-align: center;
     9         background: pink;
    10         {#line-height: 100px;#}
    11     }
    12     td{
    13         border: 1px solid;
    14         padding: 10px;
    15 
    16     }
    17     table{
    18         text-align: center;
    19     }
    20     </style>
    21 
    22 </head>
    23 <body>
    24 <ul>
    25 
    26 {% for book in books reversed %}
    27     <li>{{ book }}</li>
    28 {% endfor %}
    29 </ul>
    30 <ul>
    31 {% for v in person.values %}
    32     <li>{{ v }}</li>
    33 {% endfor %}
    34 {% for k in person.keys %}
    35     <li>{{ k }}</li>
    36 {% endfor %}
    37 {% for k,v in person.items %}
    38     <li>{{ k }}/{{ v }}</li>
    39 {% endfor %}
    40 
    41 </ul>
    42 
    43 <table>
    44     <thead>
    45     <tr>
    46         <td>从1开始序号</td>
    47         <td>从0开始序号</td>
    48         <td>反转序号最后一位是1</td>
    49         <td>反转序号最后一位是0</td>
    50         <td>书名</td>
    51         <td>作者</td>
    52         <td>价格</td>
    53     </tr>
    54     </thead>
    55     <tbody>
    56 
    57         {% for book2 in book2s %}
    58             {% if forloop.first %}
    59 {#              是否是遍历的第一行  #}
    60                 <tr style="background: red" >
    61             {% elif forloop.last %}
    62 {#                是否遍历的最后一行#}
    63                 <tr style="background: blue">
    64                 {% else %}
    65                 <tr>
    66             {% endif %}
    67             <td>{{ forloop.counter }}</td>
    68             <td>{{ forloop.counter0 }}</td>
    69             <td>{{ forloop.revcounter }}</td>
    70             <td>{{ forloop.revcounter0 }}</td>
    71             <td>{{ book2.name }}</td>
    72             <td>{{ book2.author }}</td>
    73             <td>{{ book2.price }}</td>
    74             </tr>
    75         {% endfor %}
    76 
    77 
    78     </tbody>
    79 </table>
    80 
    81 <ul>
    82     {% for comment in comments %}
    83         <li>{{ comment }}</li>
    84     {% empty %}
    85         <li>没有任何评论</li>
    86     {% endfor %}
    87 
    88 </ul>
    89 
    90 </body>
    91 </html>
    index.html
  • 相关阅读:
    归并排序
    希尔排序
    字符串操作
    引用
    直接插入排序
    变量赋值
    C#中关于公共类的使用
    关于SQL中Between语句查询日期的问题
    用户控件 与 重写控件 的区别
    什么是命名空间,为什么要使用命名空间?
  • 原文地址:https://www.cnblogs.com/randomlee/p/10291487.html
Copyright © 2011-2022 走看看