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