django:从包含的模板中访问request.get_full_path

时间:2012-03-04 18:52:37

标签: django django-templates

我在templateA.html中包含templateInclude.html。虽然我可以从templateA.html访问{{request.get_full_path}},但我无法从templateInclude.html访问。

我都想解决并理解这个问题。有没有办法将请求对象传递给包含的模板?

1 个答案:

答案 0 :(得分:5)

如果您使用的是Django 1.3+版,那么您可以使用:

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

对于早期版本,您应该使用with templatetag ie。

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

在这两种情况下,只需在您将要包含的模板中使用full_path

一般来说,它的工作方式类似于常规python代码中的上下文管理器 - http://www.python.org/dev/peps/pep-0343/

无论如何它很奇怪,因为根据文档,包含的模板可以访问父模板的整个上下文。检查您是在视图中使用RequestContext还是作为中间件使用。