Django的Context和RequestContext

参考:http://www.dannysite.com/blog/38/

Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点相似。

from django.template import Template,Context

t = Template('My name is {{ name }}.')
c = Context({'name':'Jacky'})
t.render(c)

在settings.py中有一个与RequestContext密切相关的配置项为TEMPLATE_CONTEXT_PROCESSORS。

这些Processors都会被RequestContext顺序调用,往当前Context中放入一些预定义变量。

当使用render_to_response方法时,RequestContext应作为其第三个参数传入,如:

from django.shortcuts import render_to_response, RequestContext
        
  #...
  return render_to_response('template.html', particular_data_dictionary, context_instance=RequestContext(request))
posted @ 2015-02-26 15:53  flowjacky  阅读(4382)  评论(0编辑  收藏  举报