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: http://dpaste.com/hold/806276/

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 settings.py 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?

p.s.
Why when I tried to just add, in settings.py, `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,
Giuseppe

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Pd9w4041J1cJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment