Friday, September 28, 2012

How to create a navbar and its related .current class setting

Hi, lastly, I needed to create a navbar for a project of mines compound by static and dynamic pages.
The problem I tried to solve was how to set a .current class to each item accordingly to the page visited.
Static objects were simple pages of which I knew URLs, dynamic one were simple "page" objects.

The template with which the navbar is built is the following:

As you can see, I just made an `ifequal` check between `request.path` attribute and 
the hard coded href value for my static objects, while between the page path, passed by context,
and the `page.slug` value for my dynamic "page" objects.

To pass to all templaets the `request.path` value I added in into the `TEMPLATE_CONTEXT_PROCESSORS
the value `'django.core.context_processors.request',` while to pass the path value in the class based view of
the page object, I added to its context `context['path'] = self.request.path[1:]`
Horrible, I know.

Now, while a noob, I "feel" this is not the **right** solution.
Up to you now: how would you solve the problem? Is there a best practice to solve similar issues?

Why when I tried to just add, in, `TEMPLATE_CONTEXT_PROCESSORS += ('django.core.context_processors.request',)
It didn't work? 
I needed to copy and paste the whole default context assignment, adding that to it, to avoid to get it emptied
when trying to just add that value in the above mentioned way.

Thanks you all for your help,

You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment