布尔运算符 – Django模板标签

布尔运算符 – Django模板标签

Django模板是一个使用Django模板语言标记的文本文档或Python字符串。Django是一个强大的包含电池的框架,为在模板中呈现数据提供了便利。Django模板不仅允许从视图到模板传递数据,而且还提供了一些有限的编程功能,如变量、for循环、注释、扩展、if else等。
这篇文章围绕着如何在模板中使用布尔运算符。{% if %}标签会评估一个变量,如果该变量为 “真”(即存在,不为空,也不是一个假的布尔值),则会输出该块的内容。人们可以在Django If Template标签中使用各种布尔运算符。

语法:

{% if variable boolean_operator value %}
// statements
{% endif %}

示例:
if标签可以使用和、或不来测试一些变量或否定一个给定的变量。

{% 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标签可以接受一个或几个{% elif %}子句,以及一个{% else %}子句,如果前面所有的条件都失败,就会显示出来。这些子句是可选的。

布尔运算符 – Django模板标签说明

用一个例子说明如何在Django模板中使用布尔运算符。考虑一个名为geeksforgeeks的项目,它有一个名为geeks的应用程序。

现在创建一个视图,我们将通过它来传递上下文字典。
In geeks/views.py,

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
    # create a dictionary
    context = {
        "data" : 99,
    }
    # return response
    return render(request, "geeks.html", context)

创建一个url路径来映射到这个视图。在geeks/urls.py中。

from django.urls import path
 
# importing views from views.py
from .views import geeks_view
 
urlpatterns = [
    path('', geeks_view),
]

在templates/geeks.html中创建一个模板。

{% if data == 99 %}
Value in data is : - {{ data }}
{% else %}
Data is empty
{% endif%}

让我们检查一下”/”上显示的内容是否在模板中显示。

布尔运算符 - Django模板标签

布尔操作

== 操作符
Equality. 示例:

{% if somevar == "x" %}
  This appears if variable somevar equals the string "x"
{% endif %}

!= 操作符
Inequality. 示例:

{% 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在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 %}

不在操作符
不包含在其中。这是对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 %}

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 %}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程