当前位置:K88软件开发文章中心网站服务器框架django → 文章内容

Django 模版进阶

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-25 14:19:14

文(context)对象多数时间,你可以通过传递一个完全填充(full populated)的字典给 Context() 来初始化 上下文(Context) 。 但是初始化以后,你也可以使用标准的Python字典语法(syntax)向上下文(Context) 对象添加或者删除条目:>>> from django.template import Context>>> c = Context({"foo": "bar"})>>> c['foo']'bar'>>> del c['foo']>>> c['foo']Traceback (most recent call last): ...KeyError: 'foo'>>> c['newvariable'] = 'hello'>>> c['newvariable']'hello'基本的模板标签和过滤器像我们以前提到过的,模板系统带有内置的标签和过滤器。 下面的章节提供了一个多数通用标签和过滤器的简要说明。标签if/else{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内容,例如:{% if today_is_weekend %} <p>Welcome to the weekend!</p>{% endif %}{% else %} 标签是可选的:{% if today_is_weekend %} <p>Welcome to the weekend!</p>{% else %} <p>Get back to work.</p>{% 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 %} 标签。for{% for %} 允许我们在一个序列上迭代。 与Python的 for 语句的情形类似,循环语法是 for X in Y ,Y是要迭代的序列而X是在每一个特定的循环中使用的变量名称。 每一次循环中,模板系统会渲染在 {% for %} 和{% endfor %} 之间的所有内容。例如,给定一个运动员列表 athlete_list 变量,我们可以使用下面的代码来显示这个列表:<ul>{% for athlete in athlete_list %} <li>{{ athlete.name }}</li>{% endfor %}</ul>给标签增加一个 reversed 使得该列表被反向迭代:{% for athlete in athlete_list reversed %}...{% endfor %}可以嵌套使用 {% for %} 标签:{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul>{% endfor %}在执行循环之前先检测列表的大小是一个通常的做法,当列表为空时输出一些特别的提示。{% if athlete_list %} {% for athlete in athlete_list %} <p>{{ athlete.name }}</p> {% endfor %}{% else %} <p>There are no athletes. Only computer programmers.</p>{% endif %}因为这种做法十分常见,所以for 标签支持一个可选的{% empty %} 分句,通过它我们可以定义当列表为空时的输出内容 下面的例子与之前那个等价:{% for athlete in athlete_list %} <p>{{ athlete.name }}</p>{% empty %} <p>There are no athletes. Only computer programmers.</p>{% endfor %}Django不支持退出循环操作。 如果我们想退出循环,可以改变正在迭代的变量,让其仅仅包含需要迭代的项目。 同理,Django也不支持continue语句,我们无法让当前迭代操作跳回到循环头部。 (请参看本章稍后的理念和限制小节,了解下决定这个设计的背后原因)在每个{% for %}循环里有一个称为forloop 的模板变量。这个变量有一些提示循环进度信息的属性。forloop.counter 总是一个表示当前循环的执行次数的整数计数器。 这个计数器是从1开始的,所以在第一次循环时 forloop.counter 将会被设置为1。{% for item in todo_list %} <p>{{ forloop.counter }}: {{ item }}</p>{% endfor %}forloop.counter0 类似于 forloop.counter ,但是它是从0计数的。 第一次执行循环时这个变量会被设置为0。forloop.revcounter 是表示循环中剩余项的整型变量。 在循环初次执行时 forloop.revcounter 将被设置为序列中项的总数。 最后一次循环执行中,这个变量将被置1。forloop.revcounter0 类似于 forloop.revcounter ,但它以0做为结束索引。 在第一次执行循环时,该变量会被置为序列的项的个数减1。forloop.first 是一个布尔值,如果该迭代是第一次执行,那么它被置为```` 在下面的情形中这个变量是很有用的:System Message: WARNING/2 (, line 1071); backlinkInline literal start-string without end-string.{% for object in objects %}{% if forloop.first %}<li class="first">{% else %}<li>{% en

上一页  [1] [2] [3] [4] [5] [6] [7]  下一页


Django 模版进阶