tags:

views:

26

answers:

2

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 then 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?

A: 

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".

S.Lott
That's my fisrt option, but I feel that I will repeat myself:url('^/products/$',list_view, {'path': [(_('Index'), 'index_url_name'), (_('Products'), 'products')]}, 'products')url('^/products/add/$',add_view, {'path': [(_('Index'), 'index_url_name'), (_('Products'), 'products'), (_('Add product'), 'add_product')]}, 'add_product')url('^/product/(?P<product_id>\d+)/edit/$',edit_view, {'path': [(_('Index'), 'index_url_name'), (_('Products'), 'products'), (_('Product Edition'), 'product_edition')]}, 'product_edition')This looks too repetitive, but maybe there is no better way to do this.
naw
@naw: Please don't post code in comments. Update your question.
S.Lott
+1  A: 

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.

Mark Lavin
Well, knowing that the name is "breadcrumbs" should be a great step. I'll take a look to the links to choose how to do it.
naw