參考: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))