(11)模板语言-if判断

时间:2023-03-09 01:33:16
(11)模板语言-if判断

views.py

from django.shortcuts import render,HttpResponse

# Create your views here.

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

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是首页</h1>
{{ age }} {#在页面中判断 #}
{% if age > 18 %}
<p>已经成年了</p>
{% elif age < 18 %}
<p>还没有成年</p>
{% else %}
<p>正好成年</p>
{% endif %}
</body>
</html>

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'index/', views.index)
]