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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM