Django模板(1)

  将页面的设计和Python的代码分离开会更干净简洁更容易维护。 咱们可使用 Django的 模板系统 (Template System)来实现这种模式,这就是本章要具体讨论的问题。html

  模板是一个文本,用于分离文档的表现形式和内容。 模板定义了占位符以及各类用于规范文档该如何显示的各部分基本逻辑(模板标签)。 模板一般用于产生HTML,可是Django的模板也能产生任何基于文本格式的文档。django

1. 一个简单例子数据结构

  该模板描述了一个向某个与公司签单人员致谢 HTML 页面。 可将其视为一个格式信函:app

<html>
<head><title>Ordering notice</title></head>

<body>

<h1>Ordering notice</h1>

<p>Dear {{ person_name }},</p>

<p>Thanks for placing an order from {{ company }}. It's scheduled to
ship on {{ ship_date|date:"F j, Y" }}.</p>

<p>Here are the items you've ordered:</p>

<ul>
{% for item in item_list %}
    <li>{{ item }}</li>
{% endfor %}
</ul>

{% if ordered_warranty %}
    <p>Your warranty information will be included in the packaging.</p>
{% else %}
    <p>You didn't order a warranty, so you're on your own when
    the products inevitably stop working.</p>
{% endif %}

<p>Sincerely,<br />{{ company }}</p>

</body>
</html>
View Code

代码说明:ide

  • 用两个大括号括起来的文字(例如 {{ person_name }} )称为 变量(variable) 。这意味着在此处插入指定变量的值。
  • 被大括号和百分号包围的文本(例如 {% if ordered_warranty %} )是 模板标签(template tag) 。标签(tag)定义比较明确,即: 仅通知模板系统完成某些工做的标签。
  • 这个例子中的模板包含一个for标签( {% for item in item_list %} )和一个if 标签({% if ordered_warranty %} )。for标签相似Python的for语句,可以让你循环访问序列里的每个项目。 if 标签,正如你所料,是用来执行逻辑判断的。 在这里,tag标签检查ordered_warranty值是否为True。若是是,模板系统将显示{% if ordered_warranty %}和{% else %}之间的内容;不然将显示{% else %}和{% endif %}之间的内容。{% else %}是可选的。
  • 这个模板的第二段中有一个关于filter过滤器的例子,它是一种最便捷的转换变量输出格式的方式。 如这个例子中的{{ship_date|date:”F j, Y” }},咱们将变量ship_date传递给date过滤器,同时指定参数”F j,Y”。date过滤器根据参数进行格式输出。 过滤器是用管道符(|)来调用的,具体能够参见Unix管道符

2. 使用模板系统函数

  先不和视图一块儿使用,以独立的方式使用,在Python代码中使用Django模板的最基本方式以下:lua

  • 能够用原始的模板代码字符串建立一个 Template 对象, Django一样支持用指定模板文件路径的方式来建立 Template 对象;
  • 调用模板对象的render方法,而且传入一套变量context。它将返回一个基于模板的展示字符串,模板中的变量和标签会被context值替换。
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context({'name': 'Adrian'})
>>> print t.render(c)
My name is Adrian.
>>> c = template.Context({'name': 'Fred'})
>>> print t.render(c)
My name is Fred.
View Code

   建立了Template对象,可使用context来传递数据给它。一个context是一系列变量和它们值的集合。context在Django里表现为 Context 类,在 django.template 模块里。 她的构造函数带有一个可选的参数: 一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递context来填充模板:spa

3. 同一个模板,多个上下文设计

  一旦有了 模板 对象,你就能够经过它渲染多个context, 例如:code

>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
>>> print t.render(Context({'name': 'John'}))
Hello, John
>>> print t.render(Context({'name': 'Julie'}))
Hello, Julie
>>> print t.render(Context({'name': 'Pat'}))
Hello, Pat
View Code

4. 深度变量的查找

  模板系统可以很是简洁地处理更加复杂的数据结构,例如list、dictionary和自定义的对象。在 Django 模板中遍历复杂数据结构的关键是句点字符 (.)。好比,假设你要向模板传递一个 Python 字典。 要经过字典键访问该字典的值,可以使用一个句点:

>>> from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'
View Code

  一样,也能够经过句点来访问对象的属性。 比方说, Python 的 datetime.date 对象有 year 、 month 和 day 几个属性,你一样能够在模板中使用句点来访问这些属性:

>>> from django.template import Template, Context
>>> import datetime
>>> d = datetime.date(1993, 5, 2)
>>> d.year
1993
>>> d.month
5
>>> d.day
2
>>> t = Template('The month is {{ date.month }} and the year is {{ date.year }}.')
>>> c = Context({'date': d})
>>> t.render(c)
u'The month is 5 and the year is 1993.'
View Code

  句点也可用于访问列表索引,例如:

>>> from django.template import Template, Context
>>> t = Template('Item 2 is {{ items.2 }}.')
>>> c = Context({'items': ['apples', 'bananas', 'carrots']})
>>> t.render(c)
u'Item 2 is carrots.'
View Code

  注意:不容许使用负数列表索引。 像 {{ items.-1 }} 这样的模板变量将会引起`` TemplateSyntaxError``。

5. 如何处理无效变量

  默认状况下,若是一个变量不存在,模板系统会把它展现为空字符串,不作任何事情来表示失败。 例如:

>>> from django.template import Template, Context
>>> t = Template('Your name is {{ name }}.')
>>> t.render(Context())
u'Your name is .'
>>> t.render(Context({'var': 'hello'}))
u'Your name is .'
>>> t.render(Context({'NAME': 'hello'}))
u'Your name is .'
>>> t.render(Context({'Name': 'hello'}))
u'Your name is .'
View Code

6.上下文(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'
View Code

7. 模板标签

  • if/else

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

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

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

  {% for %} 容许咱们在一个序列上迭代。 与Python的 for 语句的情形相似,循环语法是 for in Y ,Y是要迭代的序列而X是在每个特定的循环中使用的变量名称。 每一次循环中,模板系统会渲染在 {% for %} 和 {% endfor %} 之间的全部内容。

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
</ul>
View Code

  也能够嵌套使用 {% for %} 标签:

{% for athlete in athlete_list %}
    <h1>{{ athlete.name }}</h1>
    <ul>
    {% for sport in athlete.sports_played %}
        <li>{{ sport }}</li>
    {% endfor %}
    </ul>
{% endfor %}
View Code

  `` for`` 标签支持一个可选的`` {% empty %}`` 分句,经过它咱们能够定义当列表为空时的输出内容:

{% for athlete in athlete_list %}
    <h1>{{ athlete.name }}</h1>
    <ul>
    {% for sport in athlete.sports_played %}
        <li>{{ sport }}</li>
    {% endfor %}
    </ul>
{% endfor %}
View Code

  Django不支持退出循环操做。 若是咱们想退出循环,能够改变正在迭代的变量,让其仅仅包含须要迭代的项目。 同理,Django也不支持continue语句,咱们没法让当前迭代操做跳回到循环头部。

相关文章
相关标签/搜索