使用context来传递数据,一个context是一系列变量
页面设计工作和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是一系列变量的更多相关文章
- Android开发—— 传递数据
一:使用静态变量传递数据 (1)静态变量传递数据,在目标Activity中声明静态变量,然后使用setText()方法将静态变量的值导出即可: (2)静态变量传递数据,在主Activity中对目标Ac ...
- Java多线程初学者指南(7):向线程传递数据的三种方法
在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...
- react之传递数据的几种方式props传值、路由传值、状态提升、redux、context
react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...
- React之使用Context跨组件树传递数据
--------------------------------- 讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...
- 【Android】7.0 Intent向下一个活动传递数据、返回数据给上一个活动
1.0 可以利用Intent吧数据传递给上一个活动,新建一个叫“hellotest01”的项目. 新建活动FirstActivity,勾选“Generate Layout File”和“Launche ...
- Intent(三)向下一个活动传递数据
向下传递活动很简单,可以我采用putExtra()方法的重载,把我们想要传递的数据暂时放在intent中,启动活动时从这里取就可以了. 首先我们在MainActivity(主活动)显式声明intent ...
- Intent 隐式跳转,向下一个活动传递数据,向上一个活动返回数据。
一.每个Intent只能指定一个action,多个Category. 使用隐式跳转,我们不仅可以跳转到自己程序内的活动,还可以启动其他程序的活动.使得Android多个程序之间的功能共享成为可能. 例 ...
- Android开发:向下一个activity传递数据,返回数据给上一个activity
1.向下一个activity传递数据 activity1 Button button=(Button) findViewById(R.id.button1); button.setOnClickLis ...
- delphi中将一个ADOQuery查询的数据结果传递给一个动态生成的ADOQuery
delphi中将一个ADOQuery查询的数据结果传递给一个动态生成的ADOQuery 2010-03-10 17:35 方法一: beginADOQuery:=TADOQuery.Create(Ap ...
随机推荐
- SSH无密码登录配置小结
ssh-keygen -t rsa //-t指定算法 将公钥复制到被管理机器上面 ssh-copy-id -i ~/.ssh/id_rsa.pub 172.29.0.89 ssh-copy-id -i ...
- DSP using MATLAB 示例Example3.4
代码: n = [-1:3]; x = [1:5]; % x(n) = {1,2,3,4,5} % * % k = 0:500; w = (pi/500)*k; % [0,pi] axis divid ...
- psql-01基本介绍
安装与启动 安装: apt-get install postgresql / yum install postgresql.XXX; 启动: mac下直接打开 linux service postgr ...
- MFC 启动其他程序 变相跳转
尝试了多种方式之后都无法成功地在对话框程序中弹出一个单文档程序,然后我想到了这个办法. 如果直接在代码中实现某些窗口的弹出比较麻烦,可以采用这个方式来弹出这种窗口. 如果需要传递参数,只需将数据写入文 ...
- praise包--R给你点赞!
1.praise包干什么的? praise包就一个功能:赞你! 2.praise包怎么搞? 2.1安装 直接安装: install.packages("praise") 从gith ...
- varchar和Nvarchar区别
http://www.cnblogs.com/yelaiju/archive/2010/05/29/1746826.html Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字 ...
- ccc autotest
module.exports.assert = function (express,value,msg) { if(express==value) { cc.info("test:" ...
- Android 情景模式设置
情景模式的设置大家应当相当熟悉了,但是在Android中如何通过自己的程序进行情景模式的设置呢,情景模式分为多种多种,即可以使用系统自带的,也可 以使用自定义的,但是在开发某些程序时,可能需要在程序中 ...
- Jquery实现下拉联动表单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 20161004 NOIP 模拟赛 T1 解题报告
第1题 小麦亩产一千八 [问题描述] “有了金坷垃,肥料一袋能顶两袋撒,小麦亩产一千八,吸收两米下的氮磷钾……”,话说HYSBZ(Hengyang School for Boys & Zy) ...