django的学习笔记

本文详细介绍了如何在Python代码中使用模板系统,包括创建模板对象、传递变量、渲染模板等核心步骤,以及如何处理不同数据类型如字符串、日期、列表、字典和对象。此外,还展示了如何使用模板系统渲染同一模板的不同上下文,以及如何访问模板中的复杂数据结构。

如何使用模板系统

想要在Python代码中使用模板系统,只需遵循下面两个步骤:

1.可以用原始的模板代码字符串创建一个 Template 对象,Django同样支持用指定模板文件路径的方式来创建 Template 对象;

  1. 调用 Template 对象的 render() 方法并提供给他变量(i.e., 内容).它将返回一个完整的模板字符串内容,包含了所有标签块与变量解析后的内容.

模板渲染

一旦你创建一个 Template 对象,你可以用 context 来传递数据给它。一个context是一系列变量和它们值的集合。模板使用它来赋值模板变量标签和执行块标签。

context在Django里表现为 Context 类,在 django.template 模块里。 它的构造函数有一个可选参数:一个字典(映射变量和它们的值)。调用 Template 对象 的 render() 方法并传递context来填充模板:

from django.template import Context, Template
>>> t = Template("My name is {{ name }}.")
>>> c = Context({"name": "Stephane"})
>>> t.render(c)
'My name is Stephane.'

同一模板,多个上下文

一旦有了 模板 对象,你就可以通过它渲染多个背景(context),例如:

>>> 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

   
1

无论何时像这样使用同一模板源渲染多个背景,只创建 一次 模板 对象,然后对它多次调用 render() 将会更加高效。

# Bad
for name in ('John', 'Julie', 'Pat'):
    t = Template('Hello, {{ name }}')
    print t.render(Context({'name': name}))

# Good
t = Template('Hello, {{ name }}')
for name in ('John', 'Julie', 'Pat'):
    print t.render(Context({'name': name}))

在到目前为止的例子中,我们通过 context 传递的简单参数值主要是字符串,还有一个 datetime.date 范例。然而,模板系统能够非常简洁地处理更加复杂的数据结构,例如list、dictionary和自定义的对象。


 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)
'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)
'The month is 5 and the year is 1993.'
用类的实例渲染模板

from django.template import Template, Context
>>> class Person(object):
...     def __init__(self, first_name, last_name):
...         self.first_name, self.last_name = first_name, last_name
>>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')
>>> c = Context({'person': Person('John', 'Smith')})
>>> t.render(c)
'Hello, John Smith.'
最后,句点也可用于访问列表索引,

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

方法调用行为

方法调用比其他类型的查找略为复杂一点。 以下是一些注意事项:

    在方法查找过程中,如果某方法抛出一个异常,除非该异常有一个 silent_variable_failure 属性并且值为 True ,否则的话它将被传播。如果异常被传播,模板里的指定变量会被置为空字符串,比如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> t = Template( "My name is {{ person.first_name }}." )
>>> class PersonClass3:
...   def first_name( self ):
...     raise AssertionError, "foo"
>>> p = PersonClass3()
>>> t.render(Context({ "person" : p}))
Traceback (most recent call last):
...
AssertionError: foo
 
>>> class SilentAssertionError(AssertionError):
...   silent_variable_failure = True
>>> class PersonClass4:
...   def first_name( self ):
...     raise SilentAssertionError
>>> p = PersonClass4()
>>> t.render(Context({ "person" : p}))
u 'My name is .'

    仅在方法无需传入参数时,其调用才有效。 否则,系统将会转移到下一个查找类型(列表索引查找)。



内容概要:本文系统介绍了标准化和软件知识产权的基础知识,涵盖标准化的基本概念、分类、标准代号、国际标准的采用原则及程度,重点讲解了信息技术标准化、ISO与IEC等国际标准化组织以及ISO9000和ISO/IEC15504等重要标准体系;在知识产权部分,详细阐述了知识产权的定义、分类及特点,重点分析了计算机软件著作权的主体、客体、权利内容、行使方式、保护期限及侵权认定,同时涉及商业秘密的构成与侵权形式、专利权的类型与申请条件,以及企业如何综合运用著作权、专利、商标和商业秘密等方式保护软件知识产权。; 适合人群:从事软件开发、项目管理、IT标准化或知识产权相关工作的技术人员与管理人员,以及备考相关资格考试的学习者;具备一定信息技术背景,希望系统掌握标准化与软件知识产权基础知识的专业人员。; 使用场景及目标:①帮助理解各类标准的分类体系及国际标准采用方式,提升标准化实践能力;②指导企业在软件研发过程中有效保护知识产权,规避法律风险;③为软件著作权登记、专利申请、技术保密等提供理论依据和操作指引。; 阅读建议:建议结合国家相关政策法规和实际案例进行深入学习,重点关注软件著作权与专利权的适用边界、标准制定流程及企业知识产权管理策略,强化理论与实践的结合。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值