026:if标签使用详解

if标签使用详解:

if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号  {%  %}  进行包裹。 if 标签中可以使用 ==、!=、<、<=、>、>=、in、notin、is、is not 等判断运算符。示例代码如下:

一、views.py文件:

def index(request):
    return render(request, 'index.html', context={'age': 20})

二、index.html模板文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% if age < 18 %}
    <p>您是未成年人</p>
{% elif age >= 18 and age < 80 %}
    <p>您是成年人</p>
{% else %}
    <p>您是老年人</p>
{% endif %}

</body>
</html>

猜你喜欢

转载自www.cnblogs.com/Carlos-Li/p/12091201.html