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

Jos*_*ura 2 django django-templates

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

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

bx2*_*bx2 5

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

{% include "templateInclude.html" with full_path=request.get_full_path %}
Run Code Online (Sandbox Code Playgroud)

对于早期版本,您应该使用withtemplatetag ie.

{% with request.get_full_path as full_path %}
    {% include "templateInclude.html" %}
{% endwith %} 
Run Code Online (Sandbox Code Playgroud)

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

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

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