博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Definitive Guide To Django 2 学习笔记(八) 第四章 模板 (四)基本的模板标签和过滤器...
阅读量:7121 次
发布时间:2019-06-28

本文共 3797 字,大约阅读时间需要 12 分钟。

标签

下面的部分概述了常见的Django标签。

if/else

{%if%} 标签 对一个变量值进行测试,如果结果为true,系统将会显示在{%if%} 和 {%endif%}之间的一切,看个例子:

{% if today_is_weekend %}    

Welcome to the weekend!

{
% endif %} An {
% else %} tag is optional:{
% if today_is_weekend %}

Welcome to the weekend!

{
% else %}

Get back to work.

{
% endif %}

{%if%} 标签接受 and,or,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 %}

确保每个{%if%}对应一个{%endif%},否则Django将会抛出一个TemplateSyntaxError 的异常。

for

{%for%}允许你对一个序列中的每一项进行循环,格式是 for X in Y,Y是用来遍历的集合,X是变量名。每次循环,模板系统都会将{%for%}跟{%endfor%}中内容展现出来。

例如:

    {
    % for athlete in athlete_list %}
  • {
    { athlete.name }}
  • {
    % endfor %}

还可以对集合进行反转遍历:

{% for athlete in athlete_list reversed %}...{
% endfor %}

还可以进行嵌套:

{% for athlete in athlete_list %}    

{
{ athlete.name }}

    {
    % for sport in athlete.sports_played %}
  • {
    { sport }}
  • {
    % endfor %}
{
% endfor %}

 常用的一个模式是在循环遍历之前检查List的大小,如果List为空则输出一些特殊的文本

{% if athlete_list %}{
% for athlete in athlete_list %}

{

{ athlete.name }}

{
% endfor %}{
% else %}

There are no athletes. Only computer programmers.

{
% endif %}

因为这个方法太过常用,for标签支持一个可选的{%Empty%}的选项让你定义输出自定义的文本。例如:

{% for athlete in athlete_list %}

{

{ athlete.name }}

{
% empty %}

There are no athletes. Only computer programmers.

{
% endfor %}

注意,for标签不支持break,countinue。

在{%for%}标签中,你可以访问forloop变量,该变量有几个常用的属性:

1.forloop.counter:用于记录循环了多少次

2.forloop.counter0:类似forloop.counter0,只不过是从0开始的。

3.forloop.revcounter:用于记录还未被遍历的项目数

4.forloop.revcounter0:类似revcounter,不过计数是从0开始

5.forloop.first:布尔值,用来标识是否为第一个项目

6.forloop.last:布尔值,用来表示是否为最后一个项目

ifequal/ifnotequal

{%ifequal%} 标签比较两个值,如果他们相等的话,显示在{%ifequal%} 和{%endifequal%}之间的所有代码。

{% ifequal user currentuser %}

Welcome!

{
% endifequal %}

参数可以是硬编码,所以下面的例子都是有效的:

{% ifequal section 'sitenews' %}

Site News

{
% endifequal %}

{% ifequal section "community" %}

<h1>Community</h1>
{% endifequal %}

跟{%if%}类似,the {%ifequal%}标签也支持选项{%else%}

{% ifequal section 'sitenews' %}

Site News

{
% else %}

No News Here

{
% endifequal %}

只有模板变量、字符、整型,和浮点型可以作为对比参数,下面是一些有效的例子:

{% ifequal variable 1 %}{
% ifequal variable 1.23 %}{
% ifequal variable 'foo' %}{
% ifequal variable "foo" %}

其他类型如List、字典、布尔类型的都不可以作为{%ifequal%}的参数。

{% ifequal variable True %}{
% ifequal variable [1, 2, 3] %}{
% ifequal variable {
'key': 'value'} %}

这些是无效的参数。如果你要测试某些东西是否为True或False,用{%ifequal%}

注释

使用{##},多行注释则使用{%comment%}和{%endcoomment%}

 

过滤器

模板过滤是在显示他们之前变更变量值的最简单的方法。如:{

{name|lower}},这会先将name的值变为小写,然后再显示出来。

过滤器是可以多个接连使用的:

{
{ my_list|first|upper }}

 

 

转载地址:http://dvxel.baihongyu.com/

你可能感兴趣的文章
Android 四大组件之Activity 基础总结(1)
查看>>
我没有抛弃SEO,没有离开度娘,只是选择相信马云
查看>>
我的友情链接
查看>>
关于短信协议
查看>>
我的友情链接
查看>>
eclipse 编码设置
查看>>
我的友情链接
查看>>
D3介绍
查看>>
xhtml 1.0和 html 4.01的区别、规范、选择
查看>>
解决 tomcat 启动服务器内存溢出
查看>>
浏览器环境
查看>>
一步一步学习RHEL7之环境搭建
查看>>
禁用http
查看>>
关于 Tomcat 的报错:ClientAbortException
查看>>
COOKIE和SESSION保持会话
查看>>
Microsoft Azure备份VMware虚拟机_2.配置Azure Backup Server
查看>>
我的友情链接
查看>>
用python实现选择截图区域
查看>>
我的友情链接
查看>>
上班族之初体验+flexigrid关于起始加载页的笔记
查看>>