Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1606319
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: 系统运维

2012-06-11 17:09:20


点击(此处)折叠或打开

  1. from django.template import Context, Template
  2. t = Template("My name is {{ my_name }}.") #如果是字符串
  3. #t= loader.get_template('archive.html')   #载入html文件模板
  4. c = Context({"my_name": "Adrian"})
  5. t.render(c)

  6. =="My name is Adrian."
模板的基本过程是: 
步骤1. 构造模板,例如 t= Template("参数"),传入参数可以使字符串,也可以是html文件模板.
步骤2. 构造模板内容。
步骤3. 合成内容。

模板中字符类似:  {{ my_name }},由括号{{ --- }}.模板字符的命令需要注意,不能“ . ”,这在django中有特殊的意义,django遇到" . "会做查询处理,处理流程如下: 
• Dictionary lookup. Example: foo["bar"] 字典查询优先
• Attribute lookup.  Example: foo.bar
• List-index lookup. Example: foo[bar]   最后是数组查询



阅读(1191) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~