Hi,
I am new to Django and I am still going through the tutorial. I've
tried to set up a context processor for the following scenario,
and I would like to obtain some confirmation if this is a proper
solution or I am doing some mess..
In my base template with a header there is a navigation bar. A
single button should be highlighted among the others depending in
the current section / page in the website:
<ul>
<li><a href="/blog">Blog</a></li>
<li><a href="/about" class="selected">Projects</a></li>
...
</ul>
What I thought so far was to register a universal context
processor:
def navbar_selected_menu( request ):
items = {"blog": "", "about": ""}
target = request.META["PATH_INFO"]
def select( key ):
" Select the given key in the dictionary item "
nonlocal items;
items[key] = ' class="selected"';
if re.match( "(^/$)|(^/blog$)|(^/blog/)", target):
select("blog")
if re.match( "^/about\.s?html", target): # about
select("about")
return {"navbar": {"selected": items}};
and add to all links a variable such as <a href="/blog"{{ navbar.selected.blog }}>Blog</a>.
Is this solution appropriate or does it exist something clearer?
Moreover, is there the chance to pass explicitly a variable from the url mapping to the context processor ?
Kind regards,
Dean