[Dynamic Language] Python Django: 模板引擎(2)上下文Context
2010-06-17 23:03
405 查看
上下文(context)对象
Context可以通过一个完全填充(full populated)的字典来初始化,也可以使用标准的Python字典语法向其添加删除条目。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/abeen/django_test/mysite/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/django/template/context.pyc in __getitem__ (self, key)
44 if key in d:
45 return d[key]
---> 46 raise KeyError(key)
47
48 def __delitem__(self, key):
KeyError: 'foo'
代码
Context可以通过一个完全填充(full populated)的字典来初始化,也可以使用标准的Python字典语法向其添加删除条目。
In [34]: c = Context({"foo":"bar"}) In [35]: c['foo'] Out[35]: 'bar' In [36]: del c['foo'] // 删除条目 In [37]: c['foo']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/abeen/django_test/mysite/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/Django-1.2.1-py2.6.egg/django/template/context.pyc in __getitem__ (self, key)
44 if key in d:
45 return d[key]
---> 46 raise KeyError(key)
47
48 def __delitem__(self, key):
KeyError: 'foo'
代码
In [38]: c['name'] = 'ABeen' //新加条目 In [39]: c['name'] Out[39]: 'ABeen' In [40]: t = Template('my name is {{person.name}}') In [41]: c = Context({'person': c}) In [42]: t.render(c) Out[42]: u'my name is ABeen'
相关文章推荐
- Python的Django框架中的Context使用
- Python的Django框架中的Context使用
- Python的Django框架中的Context使用
- Python的Django框架中的Context使用
- 详解Python中contextlib上下文管理模块的用法
- Python异常:Django1.11--context must be a dict rather than RequestContext.
- [知识库:python-tornado]异步调用中的上下文控制Tornado stack context
- python的上下文管理(context)(1)
- Django模板HTML中for循环遍历的是上下文context的key
- python的上下文管理(contextlib)(2)
- python contextlib 上下文管理
- Python的Django框架中的Context使用
- [Dynamic Language] Python Django: 模板引擎(4)在视图中使用模板和模板继承
- Python的Django框架中的Context使用
- [Dynamic Language] Python Django: 模板引擎(1)基本使用
- [Dynamic Language] Python Django: 模板引擎(3)模板标签和过滤器
- python学习笔记:利用contextlib和@contextmanager实现with语句上下文实例
- python面对对象编程----------7:callable(类调用)与context(上下文)
- Python的Django框架中的Context使用
- java context上下文