将页面的设计和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>
代码说明:ide
2. 使用模板系统函数
先不和视图一块儿使用,以独立的方式使用,在Python代码中使用Django模板的最基本方式以下:lua
>>> 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.
建立了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
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.'
一样,也能够经过句点来访问对象的属性。 比方说, 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.'
句点也可用于访问列表索引,例如:
>>> 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.'
注意:不容许使用负数列表索引。 像 {{ 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 .'
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'
7. 模板标签
{% 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 %}
{% for %} 容许咱们在一个序列上迭代。 与Python的 for 语句的情形相似,循环语法是 for X in Y ,Y是要迭代的序列而X是在每个特定的循环中使用的变量名称。 每一次循环中,模板系统会渲染在 {% for %} 和 {% endfor %} 之间的全部内容。
<ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>
也能够嵌套使用 {% for %} 标签:
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul> {% endfor %}
`` for`` 标签支持一个可选的`` {% empty %}`` 分句,经过它咱们能够定义当列表为空时的输出内容:
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul> {% endfor %}
Django不支持退出循环操做。 若是咱们想退出循环,能够改变正在迭代的变量,让其仅仅包含须要迭代的项目。 同理,Django也不支持continue语句,咱们没法让当前迭代操做跳回到循环头部。