zoukankan      html  css  js  c++  java
  • 常用的模板标签

    常用的模板标签


    1.for

    •  1 from django.shortcuts import render, HttpResponse, redirect, reverse
       2 from datetime import datetime
       3 
       4 
       5 # Create your views here.
       6 
       7 
       8 def index(request):
       9     students = [
      10         {'name': '张三', 'age': 18, 'sex': '', 'id': 3},
      11         {'name': '李四', 'age': 19, 'sex': '', 'id': 5},
      12         {'name': '王五', 'age': 20, 'sex': '', 'id': 7},
      13         {'name': '赵六', 'age': 21, 'sex': '', 'id': 9},
      14     ]
      15     return render(request, 'crm/index.html',context={
      16                       'students': students,
      17                   })

      views视图函数

       1 <div class="container">
       2 
       3     <div class="starter-template">
       4         <table class="table">
       5             <tr>
       6 {#                <th>序号</th>#}
       7                 <th>姓名</th>
       8                 <th>年龄</th>
       9                 <th>性别</th>
      10             </tr>
      11             {% for stu in students %}
      12                 <tr>
      13                     <td>{{ stu.name }}</td>
      14                     <td>{{ stu.age }}</td>
      15                     <td>{{ stu.sex }}</td>
      16                 </tr>
      17             {% endfor %}
      18         </table>
      19     </div>
      20 
      21 </div><!-- /.container -->

      for循环使用的源码,for循环只会影响到for循环包裹的内容(到endfor)

    2.forloop

     1 <div class="container">
     2 
     3     <div class="starter-template">
     4         <table class="table">
     5             <tr>
     6                 <th>序号</th>
     7                 <th>姓名</th>
     8                 <th>年龄</th>
     9                 <th>性别</th>
    10             </tr>
    11             {% for stu in students %}
    12                 <tr>
    13                     <td>{{ forloop.counter }}</td>
    14                     <td>{{ stu.name }}</td>
    15                     <td>{{ stu.age }}</td>
    16                     <td>{{ stu.sex }}</td>
    17                 </tr>
    18             {% endfor %}
    19         </table>
    20     </div>
    21 
    22 </div><!-- /.container -->

           

    • forloop.counter:会自动记录当前的循环的迭代次数,默认从1开始。
    • forloop.counter0:从0开始记录。
    • forloop.recounter:倒序。
    • forloop.recounter0:从开始倒序。
    • forloop.first:判断当前循环是否为第一次循环,返回bool值。
    • forloop.last:判断当前循环是否为最后一次循环,返回bool值。

    3.if

    •  1 <div class="container">
       2 
       3     <div class="starter-template">
       4         <table class="table">
       5             <tr>
       6                 <th>序号</th>
       7                 <th>姓名</th>
       8                 <th>年龄</th>
       9                 <th>性别</th>
      10             </tr>
      11             {% for stu in students %}
      12                 <tr {% if stu.sex == '女' %}style="color:red" {% endif %}>
      13                     <td>{{ forloop.counter }}</td>
      14                     <td>{{ stu.name }}</td>
      15                     <td>{{ stu.age }}</td>
      16                     <td>{{ stu.sex }}</td>
      17                 </tr>
      18             {% endfor %}
      19         </table>
      20     </div>
      21 
      22 </div><!-- /.container -->

      if用法,if和for一样,需要endif来结束判断,如果符合条件,if和endif包裹的内容将会被影响

    4.url 

    • url标签一般放在href里面来动态的获取链接
    • {% url  'app_name:url_name'  参数 %}
    • 路径以字符串的形式传入(app的名字:路由名字), 如果该路由有参数,那么将参数写到标签的后面,以空格隔开。

    5.with

    • 缓存变量,(相当于Python的赋值)
  • 相关阅读:
    [HAOI2006] 旅行
    Vue 2 --v-model、局部组件和全局组件、父子组件传值、平行组件传值
    Flume简介及安装
    MySQL数据目录更改及相关问题解决方案
    更换gcc工具链
    支持多种类型的数据集合作为数据源
    23种设计模式--中介者模式-Mediator Pattern
    PID算法原理 一图看懂PID的三个参数
    内存四区
    趣味算法讲解
  • 原文地址:https://www.cnblogs.com/ivy-blogs/p/10654359.html
Copyright © 2011-2022 走看看