django url路径问题
我觉得这个问题有一个简单的答案,但我不知道。
我有一个应用程序,它使用基本模板,并带有指向用户主页“../home/”的 html 链接,但是一旦您进入网站的 2 个级别,该链接就无法返回到主页级别。
例如,用户登录并访问 www.yadda.com/home。当用户从主页选择一本书 (#35) 时,我通过 url 传递图书 ID 参数,然后转到 www.yadda.com/book/35/ 在继承自 base.html 的模板上呈现图书对象。但是,当用户想要返回主页时,原始 html 链接“../home/”(来自 base.html)会将我置于 www.yadda.com/book/home 而不是 www.yadda.com /家。
主页的绝对路径可以解决这个问题,但作为一个 django 菜鸟,我确信有一个我不熟悉的更优雅的解决方案。提前致谢。
示例代码:
urls.py
urlpatterns = patterns('booksite.views',
(r'^schedule/(\d+)/$', 'viewBook'),
(r'^home/$', 'home'), )
home.html
<a href="../book/{{s.id}}"> View This Book</a>
base.html
<a href="../index/">Home Page</a>
I feel like there is a easy answer to this but I don't know it.
I have a app that uses base template with a html link to the user's home page '../home/', but once you get 2 levels into the site, that link can't get back to the home page level.
For example, the user logs in and goes to www.yadda.com/home. When a user selects a book (#35) from the home page, I pass the book ID argument via the url and go to www.yadda.com/book/35/ rendering the book object on a template that inherits from base.html. However, when the user wants to go back to the home page, the original html link '../home/' (from base.html) puts me at www.yadda.com/book/home and not www.yadda.com/home.
An absolute path in the base to the home page would fix it, but as a django rookie, I am sure there is a more elegant solution I am unfamiliar with. Thanks in advance.
Sample Code:
urls.py
urlpatterns = patterns('booksite.views',
(r'^schedule/(\d+)/
home.html
<a href="../book/{{s.id}}"> View This Book</a>
base.html
<a href="../index/">Home Page</a>
, 'viewBook'),
(r'^home/
home.html
base.html
, 'home'), )
home.html
base.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在base.html中,不要使用
../index
,而使用/index/
。更好的是,使用
反转 URL {% url ... %}
标签。In base.html, don't use
../index
, use/index/
.Better yet, reverse your URLs using the
{% url ... %}
tag.