• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python的Django框架中if标签的相关使用

Python的Django框架中if标签的相关使用

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python django框架,python安装django,python.django,python django教程,python web django等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如:

{% if today_is_weekend %}
  <p>Welcome to the weekend!</p>
{% endif %}

</div>

{% else %} 标签是可选的:

{% if today_is_weekend %}
  <p>Welcome to the weekend!</p>
{% else %}
  <p>Get back to work.</p>
{% endif %}

</div>

Python 的“真值”

在Python和Django模板系统中,以下这些对象相当于布尔值的False

  •     空列表([] )
  •     空元组(() )
  •     空字典({} )
  •     空字符串('' )
  •     零值(0 )
  •     特殊对象None
  •     对象False(很明显)

    提示:你也可以在自定义的对象里定义他们的布尔值属性(这个是python的高级用法)。

除以上几点以外的所有东西都视为`` True``

{% if %} 标签接受 and , or 或者 not 关键字来对多个变量做判断 ,或者对变量取反( not ),例如: 例如:

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

</div>

{% if %} 标签不允许在同一个标签中同时使用 and 和 or ,因为逻辑上可能模糊的,例如,如下示例是错误的: 比如这样的代码是不合法的:

{% if athlete_list and coach_list or cheerleader_list %}

</div>

系统不支持用圆括号来组合比较操作。 如果你确实需要用到圆括号来组合表达你的逻辑式,考虑将它移到模板之外处理,然后以模板变量的形式传入结果吧。 或者,仅仅用嵌套的{% if %}标签替换吧,就像这样:

{% if athlete_list %}
  {% if coach_list or cheerleader_list %}
    We have athletes, and either coaches or cheerleaders!
  {% endif %}
{% endif %}

</div>

多次使用同一个逻辑操作符是没有问题的,但是我们不能把不同的操作符组合起来。 例如,这是合法的:

{% if athlete_list or coach_list or parent_list or teacher_list %}

</div>

并没有 {% elif %} 标签, 请使用嵌套的`` {% if %}`` 标签来达成同样的效果:

{% if athlete_list %}
  <p>Here are the athletes: {{ athlete_list }}.</p>
{% else %}
  <p>No athletes are available.</p>
  {% if coach_list %}
    <p>Here are the coaches: {{ coach_list }}.</p>
  {% endif %}
{% endif %}

</div>

一定要用 {% endif %} 关闭每一个 {% if %} 标签。

</div>

您可能想查找下面的文章:

  • Python之Web框架Django项目搭建全过程
  • Python之Web框架Django项目搭建全过程
  • python django事务transaction源码分析详解
  • python+django快速实现文件上传
  • Python+django实现简单的文件上传
  • Python+django实现简单的文件上传
  • 搭建Python的Django框架环境并建立和运行第一个App的教程
  • 搭建Python的Django框架环境并建立和运行第一个App的教程
  • Python的Django框架中消息通知的计数器实现教程
  • Python的Django框架中消息通知的计数器实现教程

相关文章

  • Centos5.x下升级python到python2.7版本教程
  • Python中除法使用的注意事项
  • 两个命令把 Vim 打造成 Python IDE的方法
  • 浅谈python新手中常见的疑惑及解答
  • Python os模块学习笔记
  • Python中使用HTMLParser解析html实例
  • Python中类的定义、继承及使用对象实例详解
  • python通过线程实现定时器timer的方法
  • python进程类subprocess的一些操作方法例子
  • Python使用迭代器捕获Generator返回值的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python检测QQ在线状态的方法
    • Python列表计数及插入实例
    • Python制作Windows系统服务
    • Python简单删除目录下文件以及文件夹的方法
    • Python可变参数函数用法实例
    • python爬虫入门教程之糗百图片爬虫代码分享
    • 使用Python实现下载网易云音乐的高清MV
    • 详解Python中表达式i += x与i = i + x是否等价
    • Python sys.argv用法实例
    • Python通过PIL获取图片主要颜色并和颜色库进行对比的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有