2012-03-04 178 views
2

我在templateA.html中包含templateInclude.html。雖然我可以從templateA.html訪問{{request.get_full_path}},但我無法從templateInclude.html中這樣做。django:從內部模板訪問request.get_full_path

我都希望解決和理解這個問題。有沒有辦法將請求對象傳遞給包含的模板?

回答

5

如果您正在使用Django版本1.3+,那麼你可以使用:

{% include "templateInclude.html" with full_path=request.get_full_path %} 

對於早期版本中,你應該使用with templatetag即。

{% with request.get_full_path as full_path %} 
    {% include "templateInclude.html" %} 
{% endwith %} 

在這兩種情況下,只是在其中將包括模板中使用full_path

一般來說它的工作原理是上下文管理器在常規的Python代碼 - http://www.python.org/dev/peps/pep-0343/

反正它是奇怪的,因爲根據文檔,包含的模板可以訪問父模板的整個來龍去脈。檢查您是否在視圖中使用RequestContext,或者作爲中間件。

+0

太好了。謝謝 :) – 2012-03-04 20:00:32