开发者

django url path problem

开发者 https://www.devze.com 2023-01-15 17:56 出处:网络
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,

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+)/$', 'viewBook'),
  (r'^home/$', 'home'), )

home.html

<a href="../book/{{s.id}}"> View This Book</a>

base.html

<a href="../index/">Home Page</a>


In base.html, don't use ../index, use /index/.

Better yet, reverse your URLs using the {% url ... %} tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消