栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

今天的Django教训汇总:template之if语法-20211029

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

今天的Django教训汇总:template之if语法-20211029

链接

if

{% if %} 标签会判断给定的变量,当变量为 True 时(比如存在、非空、非布尔值 False),就会输出块内的内容:

{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes should be out of the locker room soon!
{% else %}
    No athletes.
{% endif %}

在上面的例子中, 如果 athlete_list 不是空的, 那么变量 {{ athlete_list|length }} 就会被显示出来.

正如你所看到的,if 标签可能带有一个或多个 {% elif %} 分支,以及一个 {% else %} 分支。当 {% else %} 之前的所有分支条件都不满足时,{% else %} 分支的内容会被显示出来。所有的分支都是可选的。

布尔操作

if 标签可以使用 and、or 或 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 %}

允许在同一标签中同时使用 and 和 or 子句,and 比 or 优先级高,例如:

{% if athlete_list and coach_list or cheerleader_list %}

将被解释为:

if (athlete_list and coach_list) or cheerleader_list

在 if 标签中使用实际的括号是无效的语法。如果需要小括号来表示优先级,应使用嵌套的 if 标签。

if 标签也可以使用运算符 ==、!=、<、>、<=、>=、in、not in、is 和 `is not,其作用如下:

== 运算符
{% if somevar == "x" %}
  This appears if variable somevar equals the string "x"
{% endif %}
!= 运算符
{% if somevar != "x" %}
  This appears if variable somevar does not equal the string "x",
  or if somevar is not found in the context
{% endif %}
< 运算符
{% if somevar < 100 %}
  This appears if variable somevar is less than 100.
{% endif %}
> 运算符
{% if somevar > 0 %}
  This appears if variable somevar is greater than 0.
{% endif %}
<= 运算符
{% if somevar <= 100 %}
  This appears if variable somevar is less than 100 or equal to 100.
{% endif %}
>= 运算符¶
{% if somevar >= 1 %}
  This appears if variable somevar is greater than 1 or equal to 1.
{% endif %}
in 运算符

许多 Python 容器都支持这个运算符来测试给定值是否在容器中。下面是一些如何解释 x in y 的例子:

{% if "bc" in "abcdef" %}
  This appears since "bc" is a substring of "abcdef"
{% endif %}

{% if "hello" in greetings %}
  If greetings is a list or set, one element of which is the string
  "hello", this will appear.
{% endif %}

{% if user in users %}
  If users is a QuerySet, this will appear if user is an
  instance that belongs to the QuerySet.
{% endif %}
not in 操作符¶

不包含在其中。这是 in 运算符的取反。

is 运算符

测试两个变量是否为同一个对象。例如:

{% if somevar is True %}
  This appears if and only if somevar is True.
{% endif %}

{% if somevar is None %}
  This appears if somevar is None, or if somevar is not found in the context.
{% endif %}
is not 运算符

对象检测后取反。测试两个变量是否不是同一个对象。这是对 is 运算符的取反。例如:

{% if somevar is not True %}
  This appears if somevar is not True, or if somevar is not found in the
  context.
{% endif %}

{% if somevar is not None %}
  This appears if and only if somevar is not None.
{% endif %}
过滤器

你也可以在 if 表达式中使用过滤器。例如:

{% if messages|length >= 100 %}
   You have lots of messages today!
{% endif %}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号