📜  布尔运算符 – Django 模板标签

📅  最后修改于: 2022-05-13 01:55:09.454000             🧑  作者: Mango

布尔运算符 – Django 模板标签

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

句法:

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

例子:
if 标签可以使用 and,或不测试多个变量或否定给定变量:

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


Python3
# 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)


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


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


可以看出,if 标记可能带有一个或多个 {% elif %} 子句,以及一个 {% else %} 子句,如果前面的所有条件都失败,则会显示该子句。这些条款是可选的。

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

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

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

Python3

# 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 中,

Python3

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

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

html

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

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

if-django-模板标签

布尔运算符

==运算符
平等。例子:

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

在运算符
内含。许多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 %}

不在运算符中
不包含在内。这是 in运算符的否定。
是运算符
对象身份。测试两个值是否是同一个对象。例子:

{% 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运算符的否定。例子:

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