页面设计工作和python代码分离,所以我们引用模板来实现这个功能。

一、模板实例

下面是一个模板的实例:

[python]
<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>
<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>
分析其中的一些元素:

(1)用大括号括起来的称作:变量,需要对它进行赋值

(2)用大括号和百分号括起来的是:模板标签,即通知模板系统完成某些工作的标签。上文中有两个模板标签:for和if分别实现循环和判断。

(3)第二段有个filter过滤器的使用:

{{ ship_date|date:"F j, Y" }
它是将变量ship_date传递给过滤器date,同时指定参数,过滤器按照参数输出。
二、使用模板

1、基本流程:

(1)用原始的模板代码创建一个Template对象

(2)创建一个Context对象,完成对模板对象中的变量赋值

(3)调用模板对象的render方法,将(2)步生成的context对象作为参数,填充模板,同时返回基于模板的展现字符串,这时其中的变量已经被替换。

实例:

[python]
>>> 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.
>>> 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.
2、创建模板对象

转到mysite所在的目录,输入

[python]
python manage.py shell
python manage.py shell
为什么我们运行pythonmanage.pyshell而不是python的。这两个命令都会启动交互解释器,但是manage.pyshell命令有一个重要的不同: 在启动解释器之前,它告诉Django使用哪个设置文件。 Django框架的大部分子系统,包括模板系统,都依赖于配置文件;如果Django不知道使用哪个配置文件,这些系统将不能工作。
[python]
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> print t
<django.template.base.Template object at 0x0220EBD0>
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> print t
<django.template.base.Template object at 0x0220EBD0>每次创建一个template对象,打印出来的地址都不同。

3、模板渲染

即,对模板内的变量、标签赋值。

使用context来传递数据,一个context是一系列变量和他们的值的集合,然后用template的render方法传递context填充模板。

[python]
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context('name': 'Jim')
>>> t.render(c)2881064151
u'My name is Jim.'
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context('name': 'Jim')
>>> t.render(c)
u'My name is Jim.'
实例:
[python]
>>> from django.template import Template, Context
>>> raw_template = """<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>
...
... {% 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>"""
>>> t = Template(raw_template)
>>> import datetime
>>> c = Context({'person_name': 'John Smith',
... 'company': 'Outdoor Equipment',
... 'ship_date': datetime.date(2009, 4, 2),
... 'ordered_warranty': False})
>>> t.render(c)
u"<p>Dear John Smith,</p>\n\n<p>Thanks for placing an order from Outdoor
Equipment. It's scheduled to\nship on April 2, 2009.</p>\n\n\n<p>You
didn't order a warranty, so you're on your own when\nthe products
inevitably stop working.</p>\n\n\n<p>Sincerely,<br />Outdoor Equipment
</p>"
>>> from django.template import Template, Context
>>> raw_template = """<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>
...
... {% 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>"""
>>> t = Template(raw_template)
>>> import datetime
>>> c = Context({'person_name': 'John Smith',
... 'company': 'Outdoor Equipment',
... 'ship_date': datetime.date(2009, 4, 2),
... 'ordered_warranty': False})
>>> t.render(c)
u"<p>Dear John Smith,</p>\n\n<p>Thanks for placing an order from Outdoor
Equipment. It's scheduled to\nship on April 2, 2009.</p>\n\n\n<p>You
didn't order a warranty, so you're on your own when\nthe products
inevitably stop working.</p>\n\n\n<p>Sincerely,<br />Outdoor Equipment
</p>"

输出的结果没有按照\n显示换行,这和python解释器有关,它会按照真实内容显示,如果想显示出换行,使用print t.render(c)

4、一个模板多个渲染
一旦定义了一个模板,就可以渲染多个context
[python]
>>> from django.template import Template, Context
>>> t = Template('My name is {{ name }}.')
>>> print t.render(Context{'name': 'Jim'})
My name is Jim.
>>> print t.render(Context{'name': 'Pat'})
My name is Pat.
>>> from django.template import Template, Context
>>> t = Template('My name is {{ name }}.')
>>> print t.render(Context{'name': 'Jim'})
My name is Jim.
>>> print t.render(Context{'name': 'Pat'})
My name is Pat.
[python]
>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
>>> for name in ('John', 'Juile', 'pat'):
... print t.render(Context({'name': name}))
...
Hello, John
Hello, Juile
Hello, pat
>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
>>> for name in ('John', 'Juile', 'pat'):
... print t.render(Context({'name': name}))
...
Hello, John
Hello, Juile
Hello, pat

使用context来传递数据,一个context是一系列变量的更多相关文章

  1. Android开发—— 传递数据

    一:使用静态变量传递数据 (1)静态变量传递数据,在目标Activity中声明静态变量,然后使用setText()方法将静态变量的值导出即可: (2)静态变量传递数据,在主Activity中对目标Ac ...

  2. Java多线程初学者指南(7):向线程传递数据的三种方法

    在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...

  3. react之传递数据的几种方式props传值、路由传值、状态提升、redux、context

    react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...

  4. React之使用Context跨组件树传递数据

    ---------------------------------  讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...

  5. 【Android】7.0 Intent向下一个活动传递数据、返回数据给上一个活动

    1.0 可以利用Intent吧数据传递给上一个活动,新建一个叫“hellotest01”的项目. 新建活动FirstActivity,勾选“Generate Layout File”和“Launche ...

  6. Intent(三)向下一个活动传递数据

    向下传递活动很简单,可以我采用putExtra()方法的重载,把我们想要传递的数据暂时放在intent中,启动活动时从这里取就可以了. 首先我们在MainActivity(主活动)显式声明intent ...

  7. Intent 隐式跳转,向下一个活动传递数据,向上一个活动返回数据。

    一.每个Intent只能指定一个action,多个Category. 使用隐式跳转,我们不仅可以跳转到自己程序内的活动,还可以启动其他程序的活动.使得Android多个程序之间的功能共享成为可能. 例 ...

  8. Android开发:向下一个activity传递数据,返回数据给上一个activity

    1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...

  9. delphi中将一个ADOQuery查询的数据结果传递给一个动态生成的ADOQuery

    delphi中将一个ADOQuery查询的数据结果传递给一个动态生成的ADOQuery 2010-03-10 17:35 方法一: beginADOQuery:=TADOQuery.Create(Ap ...

随机推荐

  1. loadrunner取出字符串的后面几位

    Action() {    char *phonenum;    int k=1;    phonenum=lr_eval_string("{phoneNum}");//参数化获取 ...

  2. HTML5 重要标签及其属性学习

    1.google字体:<link href="https://fonts.googleapis.com/css?family=Lobster" rel="style ...

  3. Swift3.0语言教程比较、判断字符串

    Swift3.0语言教程比较.判断字符串 Swift3.0语言教程比较.判断字符串,在一个程序中字符串很多时,常常会做的操作就是对这些字符串进行比较和判断.本小节将讲解这些内容. 1.不区分大小写比较 ...

  4. 上传文件大于 2G以上

    1. 开始->运行中输入以下路径, 回车. %windir%\system32\inetsrv\config\applicationhost.config 2. 在打开的配置文件中搜索" ...

  5. 【转】linux network namespace 学习

    原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...

  6. 向量时钟Vector Clock in Riak

    Riak 是以 Erlang 编写的一个高度可扩展的分布式数据存储,Riak的实现是基于Amazon的Dynamo论文,Riak的设计目标之一就是高可用.Riak支持多节点构建的系统,每次读写请求不需 ...

  7. EF框架step by step(3)—Code-First

    CodeFirst是EF框架的第三种方式,也是最为复杂一种方式,本文将以EF4.1版本为基础,简要讲解一下用法,同时,也介绍DbContext的用法. 本文采用的示例仍然是前两篇采用的博客用户的示例. ...

  8. BZOJ 1475 & 1324 && 建图最小割

    题意: 给一个矩阵,取其中一方格中的数,满足所有所取方格不相邻. SOL: 典型一个二分图,染色后不相邻的连边即可.跑个最大流,裸裸哒. Code: 代码没什么时间写了...并不是很想贴...都是贴板 ...

  9. 【NOI2016】区间 题解

    题目大意: 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. 思路: 按区间的长度从小到大排序,可知连续的几个区间最优,则用两个指针指其头尾,线性扫描,再用线段树区间 ...

  10. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...