开发者

In Django, how to define a "location path" in order to display it to the users?

I want to put a "location path" in my pages showing where the user is. Supposing that the user is watching one product, it could be Index > Products > ProductName where each word is also a link to other pages.

I was thinking on passing to the template a variable with the path like [(_('Index'), 'index_url_name'), (_('Products'), 'products_list_url_name'), (_('ProductName'), 'product_url_name')]

But 开发者_如何学Cthen I wonder where and how would you define the hierarchy without repeating myself (DRY)?

As far I know I have seen two options

  • To define the hierarchy in the urlconf. It could be a good place since the URL hierarchy should be similar to the "location path", but I will end repeating fragments of the paths.
  • To write a context processor that guesses the location path from the url and includes the variable in the context. But this would imply to maintain a independient hierarchy wich will need to be kept in sync with the urls everytime I modify them.

Also, I'm not sure about how to handle the urls that require parameters.

Do you have any tip or advice about this? Is there any canonical way to do this?


There are a number of snippets and reusable apps to do breadcrumbs in Django. Here are a few to look at.

Snippets:
http://djangosnippets.org/snippets/1487/
http://djangosnippets.org/snippets/1026/

Apps:
http://code.google.com/p/django-breadcrumbs/
http://code.google.com/p/django-crumbs/

Hopefully one of them will work for you or at least get you going in the right direction.


The "hierarchy" is usually implied by the URLs.

You can -- trivially -- add information in the urls.py that is passed to the view function. That can be passed to the template by the view function.

See http://docs.djangoproject.com/en/1.2/topics/http/urls/#patterns

Provide the "optional dictionary" in your urls with path information.

You can also do this.

url( r'/(?P<level1>path)/(?P<level2>to)/(?P<level3>resource)/', ... )

This might save some "repetition".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜