在模板文件中,还能嵌套入模板标签,做一些特殊处理,例如流程控制,下面将简单介绍下模板标签,主要介绍if和for 标签。
参考官方文档 : https://docs.djangoproject.com/en/1.5/ref/templates/builtins/
一、for 标签
循环遍历数组中的每个元素,例如,展示athletes 列表中的元素:
    {%forathleteinathlete_list%}
  • {{athlete.name}}
  • {%endfor%}
你可以通过for 标签 ,通过{% for  obj  in  list  reversed %} 遍历列表中的所有元素。
例子1:
[root@node1 webproject]# vim jobs/views.py
book_list = ['python', 'django', 'perl', 'php']
[root@node1 webproject]# vim jobs/templates/index.html
   
       {% for bname in book_list %}
               
  • `bname`
  •        {% endfor %}
       


    Django 学习笔记(三)_第1张图片

    如果你需要遍历列表中多个列,可以把每个子列解包成单独的列。例如,如果是一个内容包含(x,y)两个元素叫做points 的列表,你可以通过以下方法输出points的列:
    {%forx,yinpoints%}
        There is a point at {{x}},{{y}}{%endfor%}
    这种方法同样适用于当你需要遍历字典键与键值的情况。例如,如果有个data 字典,可以通过下面的方法同时输出键和键值:
    {%forkey,valueindata.items%}{{key}}: {{value}}{%endfor%}
    例子2:
    [root@node1 webproject]# vim jobs/views.py
    user = {'name':'pmghong', 'age':23, 'sex':'male'}
    [root@node1 webproject]# vim jobs/templates/index.html
       
           {% for key, value in user.items %}
                   
  • `key`: `value`
  •        {% endfor %}
       


    214701817.png


    for  还能为每一列设置序号,例如:
    Variable Description
    forloop.counter 显示序号(从1开始)
    forloop.counter0 显示序号(从0开始)
    forloop.revcounter 显示序号(倒序,从1开始)
    forloop.revcounter0 显示序号(倒序,从0开始)
    forloop.first 如果元素是第一个元素,则返回值为True
    forloop.last 如果元素师最后一个元素,则返回值为True
    forloop.parentloop 如果是一个嵌套的循环,那么引用外面的forloop遍历
    例子3:
    [root@node1 webproject]# vim jobs/templates/index.html
  • `forloop`.`counter`. `key`: `value`

  • 214757786.png


    for ... empty
    for 标签有一个可选的选项{% empty %} 用于处理没有元素或找不到元素的情况:
      {%forathleteinathlete_list%}
    • {{athlete.name}}
    • {%empty%}
    • Sorry, no athlete in this list!
    • {%endfor%}
      或者
        {%ifathlete_list%}{%forathleteinathlete_list%}
      • {{athlete.name}}
      • {%endfor%}{%else%}
      • Sorry, no athletes in this list.
      • {%endif%}
      第一种写法相对于第二种更加简洁,简短还有更高效。
      例子4:
         
             {% for athlete in athlete_list %}
                 
    • ` athlete`.`name `
    • {% empty %}
                 
    • Sorry, no athlete in this list!
    •        {% endfor %}
         


      214816865.png


      二、if 标签
      if 标签用作判断,如果值为True(存在,不为空,布尔值不是false)则执行后面的代码块
      {%ifathlete_list%}
          Number of athletes: {{athlete_list|length}}{%elifathlete_in_locker_room_list%}
          Athletes should be out of the locker room soon!
      {%else%}
          No athletes.
      {%endif%}
      上面的例子中,如果athlete 的不为空,则显示athlete 的数目。
      我们可以看到,if 标签能够支持一个或多个{% elif %} 子句,而{% else %} 后面的代码块将在无法匹配到任何条件的情况下执行。这些子句都是可选的。
      注意:{% elif %} 子句从Django 1.4.x 以后的版本开始支持。
      布尔值操作
      if 标签能使用and,or还有not 来测试变量的值或否定变量的值
      {%ifathlete_listandcoach_list%}
          Both athletes and coaches are available.
      {%endif%}{%ifnotathlete_list%}
          There are no athletes.
      {%endif%}{%ifathlete_listorcoach_list%}
          There are some athletes or some coaches.
      {%endif%}{%ifnotathlete_listorcoach_list%}
          There are no athletes or there are some coaches (OK, so
          writing English translations of boolean logic sounds
          stupid; it's not our fault).
      {%endif%}{%ifathlete_listandnotcoach_list%}
          There are some athletes and absolutely no coaches.
      {%endif%}
      同时使用and 和 or 是允许的,其中and 的优先级更高,例如
      {%ifathlete_listandcoach_listorcheerleader_list%}
      等同于:
      if(athlete_listandcoach_list)orcheerleader_list
      实际上,使用中括号是无效的语法标记。如果你需要他们来表示优先级,你应该使用嵌套标签。
      if 标签还支持==,!=,<,>,<=,>= 和in 的操作:
      (1) ==
      {%ifsomevar=="x"%}
        This appears if variable somevar equals the string "x"
      {%endif%}
      (2) !=
      {%ifsomevar!="x"%}
        This appears if variable somevar does not equal the string "x",
        or if somevar is not found in the context
      {%endif%}
      (3)<,>
      {%ifsomevar<100%}
        This appears if variable somevar is less than 100.
      {%endif%}
      {%ifsomevar>0%}
        This appears if variable somevar is greater than 0.
      {%endif%}
      (4)<=,>=
      {%ifsomevar<=100%}
        This appears if variable somevar is less than 100 or equal to 100.
      {%endif%}
      {%ifsomevar>=1%}
        This appears if variable somevar is greater than 1 or equal to 1.
      {%endif%}
      (5) in
      很多python 容器支持这个操作,用于判断提供的值是否包含在容器中,例如下面的例子:
      {%if"bc"in"abcdef"%}
        This appears since "bc" is a substring of "abcdef"
      {%endif%}{%if"hello"ingreetings%}
        If greetings is a list or set, one element of which is the string
        "hello", this will appear.
      {%endif%}{%ifuserinusers%}
        If users is a QuerySet, this will appear if user is an
        instance that belongs to the QuerySet.
      {%endif%}
      (6)not in
      表示不包含在容器中,这是一个否定的操作。
      比较运算符不能像在Python或数学符号中“链接”,例如:
      {%ifa>b>c%}  (错误的)
      正确的表达式,应该这样:
      {%ifa>bandb>c%}
      你也可以在表达式中使用过滤器,例如:
      {%ifmessages|length>=100%}
         You have lots of messages today!
      {%endif%}