最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-27 14:42:12
文档

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

Python的Django框架中if标签的相关使用:{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如: {% if today_is_weekend %} Welcome to the weekend! {% e
推荐度:
导读Python的Django框架中if标签的相关使用:{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如: {% if today_is_weekend %} Welcome to the weekend! {% e

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

{% if today_is_weekend %}
 

Welcome to the weekend!

{% endif %}

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

{% if today_is_weekend %}
 

Welcome to the weekend!

{% else %}

Get back to work.

{% endif %}

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

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

    {% if athlete_list and coach_list or cheerleader_list %}
    
    

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

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

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

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

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

    {% if athlete_list %}
     

    Here are the athletes: {{ athlete_list }}.

    {% else %}

    No athletes are available.

    {% if coach_list %}

    Here are the coaches: {{ coach_list }}.

    {% endif %} {% endif %}

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

    文档

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

    Python的Django框架中if标签的相关使用:{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如: {% if today_is_weekend %} Welcome to the weekend! {% e
    推荐度:
    标签: 标签 使用 标签的
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top