zoukankan      html  css  js  c++  java
  • Python框架之Django学习笔记(七)

    标签

    eif/else

      {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如:

    1 {% if today_is_weekend %}
    2     <p>Welcome to the weekend!</p>
    3 {% endif %}

      {% else %} 标签是可选的:

    1 {% if today_is_weekend %}
    2     <p>Welcome to the weekend!</p>
    3 {% else %}
    4     <p>Get back to work.</p>
    5 {% endif %}

    Python 的“真值”

       在Python和Django模板系统中,以下这些对象相当于布尔值的False:
    • 空列表([] )

    • 空元组(() )

    • 空字典({} )

    • 空字符串('' )

    • 零值(0 )

    • 特殊对象None

    • 对象False(很明显)

      除以上几点以外的所有东西都视为"True"。

      {% if %} 标签接受 and , or 或者 not 关键字来对多个变量做判断 ,或者对变量取反( not ),例如:

    {% if athlete_list and coach_list %}
        Both athletes and coaches are available.
    {% endif %}
    
    {% if not athlete_list %}
        There are no athletes.
    {% endif %}
    
    {% if athlete_list or coach_list %}
        There are some athletes or some coaches.
    {% endif %}
    
    {% if not athlete_list or coach_list %}
        There are no athletes or there are some coaches.
    {% endif %}
    
    {% if athlete_list and not coach_list %}
        There are some athletes and absolutely no coaches.
    {% endif %}

      {% if %} 标签不允许在同一个标签中同时使用 and 和 or ,因为逻辑上可能模糊的,例如,如下示例是错误的: 比如这样的代码是不合法的:

    {% if athlete_list and coach_list or cheerleader_list %}

      系统不支持用圆括号来组合比较操作。 如果你确实需要用到圆括号来组合表达你的逻辑式,考虑将它移到模板之外处理,然后以模板变量的形式传入结果吧。 或者,仅仅用嵌套的{% if %}标签替换吧,就像这样:

    1 {% if athlete_list %}
    2     {% if coach_list or cheerleader_list %}
    3         We have athletes, and either coaches or cheerleaders!
    4     {% endif %}
    5 {% endif %}

      多次使用同一个逻辑操作符是没有问题的,但是我们不能把不同的操作符组合起来。 例如,这是合法的:

    {% if athlete_list or coach_list or parent_list or teacher_list %}

      并没有 {% elif %} 标签, 请使用嵌套的`` {% if %}`` 标签来达成同样的效果:

    1 {% if athlete_list %}
    2     <p>Here are the athletes: {{ athlete_list }}.</p>
    3 {% else %}
    4     <p>No athletes are available.</p>
    5     {% if coach_list %}
    6         <p>Here are the coaches: {{ coach_list }}.</p>
    7     {% endif %}
    8 {% endif %}

      PS:一定要用 {% endif %} 关闭每一个 {% if %} 标签。

    for

      {% for %} 允许我们在一个序列上迭代。 与Python的 for 语句的情形类似,循环语法是 for X in Y ,Y是要迭代的序列而X是在每一个特定的循环中使用的变量名称。 每一次循环中,模板系统会渲染在 {% for %} 和{% endfor %} 之间的所有内容。

      例如,给定一个运动员列表 athlete_list 变量,我们可以使用下面的代码来显示这个列表:

    1 <ul>
    2 {% for athlete in athlete_list %}
    3     <li>{{ athlete.name }}</li>
    4 {% endfor %}
    5 </ul>

      给标签增加一个 reversed 使得该列表被反向迭代:

     {% for athlete in athlete_list reversed %} ... {% endfor %} 

      可以嵌套使用 {% for %} 标签:

    1 {% for athlete in athlete_list %}
    2     <h1>{{ athlete.name }}</h1>
    3     <ul>
    4     {% for sport in athlete.sports_played %}
    5         <li>{{ sport }}</li>
    6     {% endfor %}
    7     </ul>
    8 {% endfor %}

      在执行循环之前先检测列表的大小是一个通常的做法,当列表为空时输出一些特别的提示。

    1 {% if athlete_list %}
    2     {% for athlete in athlete_list %}
    3         <p>{{ athlete.name }}</p>
    4     {% endfor %}
    5 {% else %}
    6     <p>There are no athletes. Only computer programmers.</p>
    7 {% endif %}

      因为这种做法十分常见,所以“ for” 标签支持一个可选的“{% empty %}” 分句,通过它我们可以定义当列表为空时的输出内容 下面的例子与之前那个等价:

    1 {% for athlete in athlete_list %}
    2     <p>{{ athlete.name }}</p>
    3 {% empty %}
    4     <p>There are no athletes. Only computer programmers.</p>
    5 {% endfor %}

      Django不支持退出循环操作。 如果我们想退出循环,可以改变正在迭代的变量,让其仅仅包含需要迭代的项目。 同理,Django也不支持continue语句,我们无法让当前迭代操作跳回到循环头部。 

      在每个“ {% for %}”循环里有一个称为“ forloop” 的模板变量。这个变量有一些提示循环进度信息的属性。

      forloop.counter 总是一个表示当前循环的执行次数的整数计数器。 这个计数器是从1开始的,所以在第一次循环时 forloop.counter 将会被设置为1。

    1 {% for item in todo_list %}
    2     <p>{{ forloop.counter }}: {{ item }}</p>
    3 {% endfor %}

      forloop.counter0 类似于 forloop.counter ,但是它是从0计数的。 第一次执行循环时这个变量会被设置为0。

      forloop.revcounter 是表示循环中剩余项的整型变量。 在循环初次执行时 forloop.revcounter 将被设置为序列中项的总数。 最后一次循环执行中,这个变量将被置1。

      forloop.revcounter0 类似于 forloop.revcounter ,但它以0做为结束索引。 在第一次执行循环时,该变量会被置为序列的项的个数减1。

      forloop.first 是一个布尔值,如果该迭代是第一次执行,那么它被置为“” 在下面的情形中这个变量是很有用的:  

      System Message: WARNING/2 (<string>, line 1071); backlink

      Inline literal start-string without end-string.

    1 {% for object in objects %}
    2     {% if forloop.first %}<li class="first">{% else %}<li>{% endif %}
    3     {{ object }}
    4     </li>
    5 {% endfor %}

      forloop.last 是一个布尔值;在最后一次执行循环时被置为True。 一个常见的用法是在一系列的链接之间放置管道符(|)

    1 {% for link in links %}{{ link }}{% if not forloop.last %} | {% endif %}{% endfor %}

      上面的模板可能会产生如下的结果:

    1 Link1 | Link2 | Link3 | Link4

      另一个常见的用途是为列表的每个单词的加上逗号。 

    1 Favorite places:
    2 {% for p in places %}{{ p }}{% if not forloop.last %}, {% endif %}{% endfor %}

      forloop.parentloop 是一个指向当前循环的上一级循环的 forloop 对象的引用(在嵌套循环的情况下)。 例子在此:

     1 {% for country in countries %}
     2     <table>
     3     {% for city in country.city_list %}
     4         <tr>
     5         <td>Country #{{ forloop.parentloop.counter }}</td>
     6         <td>City #{{ forloop.counter }}</td>
     7         <td>{{ city }}</td>
     8         </tr>
     9     {% endfor %}
    10     </table>
    11 {% endfor %}

      forloop 变量仅仅能够在循环中使用。 在模板解析器碰到{% endfor %}标签后,forloop就不可访问了。

      Context和forloop变量

      在一个 {% for %} 块中,已存在的变量会被移除,以避免 forloop 变量被覆盖。 Django会把这个变量移动到forloop.parentloop 中。通常我们不用担心这个问题,但是一旦我们在模板中定义了 forloop 这个变量(当然我们反对这样做),在 {% for %} 块中它会在 forloop.parentloop 被重新命名。

      ifequal/ifnotequal

      Django模板系统压根儿就没想过实现一个全功能的编程语言,所以它不允许我们在模板中执行Python的语句(还是那句话,要了解更多请参看理念和限制小节)。 但是比较两个变量的值并且显示一些结果实在是个太常见的需求了,所以Django提供了 {% ifequal %} 标签供我们使用。

      {% ifequal %} 标签比较两个值,当他们相等时,显示在 {% ifequal %} 和 {% endifequal %} 之中所有的值。

      下面的例子比较两个模板变量 user 和 currentuser : 

    1 {% ifequal user currentuser %}
    2     <h1>Welcome!</h1>
    3 {% endifequal %}

      参数可以是硬编码的字符串,随便用单引号或者双引号引起来,所以下列代码都是正确的:

    1 {% ifequal section 'sitenews' %}
    2     <h1>Site News</h1>
    3 {% endifequal %}
    4 
    5 {% ifequal section "community" %}
    6     <h1>Community</h1>
    7 {% endifequal %}

      和 {% if %} 类似, {% ifequal %} 支持可选的 {% else%} 标签:

    1 {% ifequal section 'sitenews' %}
    2     <h1>Site News</h1>
    3 {% else %}
    4     <h1>No News Here</h1>
    5 {% endifequal %}

      只有模板变量,字符串,整数和小数可以作为 {% ifequal %} 标签的参数。下面是合法参数的例子:

    1 {% ifequal variable 1 %}
    2 {% ifequal variable 1.23 %}
    3 {% ifequal variable 'foo' %}
    4 {% ifequal variable "foo" %}

      其他任何类型,例如Python的字典类型、列表类型、布尔类型,不能用在 {% ifequal %} 中。 下面是些错误的例子:

    1 {% ifequal variable True %}
    2 {% ifequal variable [1, 2, 3] %}
    3 {% ifequal variable {'key': 'value'} %}

      如果你需要判断变量是真还是假,请使用 {% if %} 来替代 {% ifequal %} 。

      注释

      就像HTML或者Python,Django模板语言同样提供代码注释。 注释使用 {# #} :

    1 {# This is a comment #}  

      注释的内容不会在模板渲染时输出。

      用这种语法的注释不能跨越多行。 这个限制是为了提高模板解析的性能。 在下面这个模板中,输出结果和模板本身是 完全一样的(也就是说,注释标签并没有被解析为注释):

    1 This is a {# this is not
    2 a comment #}
    3 test.

      如果要实现多行注释,可以使用`` {% comment %}`` 模板标签,就像这样:

    1 {% comment %}
    2 This is a
    3 multi-line comment.
    4 {% endcomment %}
    评论
  • 相关阅读:
    为什么越来越少的人用jQuery
    远程工作是一种怎样的体验?
    JavaScript黑客是这样窃取比特币的,Vue开发者不用担心!
    原有vue项目接入typescript
    ES6核心特性
    强烈推荐10个值得订阅的国外技术周报
    jquery ajax超时设置(转载)
    Multiple “order by” in LINQ(转载)
    Cookies, Claims and Authentication in ASP.NET Core(转载)
    Json.NET序列化后包含类型,保证序列化和反序列化的对象类型相同(转载)
  • 原文地址:https://www.cnblogs.com/voidy/p/3960323.html
Copyright © 2011-2022 走看看