模板中的读取路径 [英] Reading path in templates
问题描述
有没有办法读取当前页面的路径?例如,我在 www.example.com/foo/bar/- 我想阅读/foo/bar/".我必须在模板文件中这样做而不修改视图,而且我的视图文件太多,无法逐个编辑.
is there any way to read the path to the current page? For example, I am at www.example.com/foo/bar/ - and I want to read '/foo/bar/'. I have to do this in the template file without modifying views, and I have too many view files to edit each one.
干杯.
推荐答案
如果您将 django.core.context_processors.request
添加到您的 TEMPLATE_CONTEXT_PROCESSORS
设置中,它将添加request
变量用于每个使用 RequestContext
(这是大多数内置的)的模板渲染.这是当前请求的 HTTPRequest
对象,其 path
属性是请求的路径.更多信息可以在中找到文档.
If you add django.core.context_processors.request
to your TEMPLATE_CONTEXT_PROCESSORS
setting, it will add the request
variable to every template-rendering that uses a RequestContext
(which is most of the built-in ones). This is the HTTPRequest
object for the current request, the path
attribute of which is the requested path. More information can be found in the docs.
这篇关于模板中的读取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!