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

Posted by naw on Stack Overflow See other posts from Stack Overflow or by naw
Published on 2010-05-20T17:22:17Z Indexed on 2010/05/20 18:40 UTC
Read the original article Hit count: 215

Filed under:

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?

© Stack Overflow or respective owner

Related posts about django