DTL中的IF

代码1,使用if判断大小

views.py文件:
from django.shortcuts import render
def index(request):
context = {
“age”:17
}
return render(request,‘index.html’,context=context)

index.html文件:

Title {% if age < 18 %}

你是未成年人不可以进入网吧

{% elif age == 18 %}

你已经18岁了可以进入网吧了

{% else %}

你已经是成年人了,需要承担家庭的责任

{% endif %}

代码2 if in:

views.py文件:
from django.shortcuts import render

def index(request):
context = {
“heros”:[
“关羽”,
“赵云”
]
}
return render(request,‘index.html’,context=context)

index.html文件:

Title {% if "项羽" in heros %}

项羽待命

{% else %}

项羽下线了

{% endif %}

猜你喜欢

转载自blog.csdn.net/jiating167168/article/details/89226620