模板中的读取路径 [英] Reading path in templates

查看:25
本文介绍了模板中的读取路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法读取当前页面的路径?例如,我在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆