Django块标签if else 配合not and or的使用

来源:互联网 发布:淘宝怎么添加宝贝分类 编辑:程序博客网 时间:2024/06/05 04:10

Django块标签if else 配合not and or的使用

模板文件index1.html中的代码如下:

<html lang="en">    <head>    <meta charset="UTF-8">    <title>wuranghao</title>    </head>    <body>    <h1>hello:{{user.name}}</h1>    <h1>age:{{user.age}}</h1>    <h1>male:{{user.sex}}</h1>    {%  if user.age > 18  %}        <li>未成年</li>    {% endif %}     {%  if not user2   %}        <li>user2是不存在的</li>    {% endif %}     {%  if not user.name   %}        <li>user.name是为空的</li>    {% endif %}     {%  if not user2  or not user.name   %}        <li>user2是不存在的或者是user.name是为空的</li>    {% endif %}     </body></html>

views.py中的代码如下:

# Create your views here.from django.http import HttpResponsefrom django.template import loader,Contextclass Person(object):    def __init__(self,name,age,sex):        self.name=name        self.age=age        self.sex=sex    def getName(self):        return "my name is  "+self.namedef index1(request):    t=loader.get_template("index1.html")    person=Person("wuranghao",19,"male")    c=Context({"user":person})    return HttpResponse(t.render(c))

运行结果如下:

0 0
原创粉丝点击