2012-02-27 28 views

回答

4

這是上下文處理器的用途 - 您可以定義一個字典,將其添加到每個響應的上下文中。

它們寫起來非常簡單,只需將它們添加到設置中即可。請記住確保您使用的是RequestContext,而不是用於模板的純上下文,並且每次都會自動運行。

文檔是在這裏:https://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors

+2

FWIW,如果你使用'render'方法或者基於類的意見(均在Django 1.3+),在'RequestContext'位爲你隱式進行。 – 2012-02-27 18:34:20

1

您可以使用一個上下文處理器,如this example看到。每個render_to_response最終會看起來像:

return render_to_response("my_app/my_template.html", {'some_var': 'foo'}, 
         context_instance=RequestContext(request)) 
相關問題