I
I
Isaac Clark2012-08-03 17:00:48
Django
Isaac Clark, 2012-08-03 17:00:48

How to add the right class to the menu item depending on the URL?

Hello.
Tell me please.
There is a menu on the site: HOME - NEWS - CONTACTS
When you click on any link of this menu, the desired item in the menu is painted red. How to do it?
It is necessary to change something in urls.py or views.py and what exactly please tell me.

#views.py
def Owners(request):  
    return render_to_response('base.html')

#urls.py
urlpatterns = patterns('owners.views',
    url(r'^$', 'Owners'),
)
</sourse>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mike_k, 2012-08-04
@Dark_Knight

views.py:

from django.shortcuts import render, get_object_or_404
def someview(request, slug=None):
    some_obj = 
    return render(
        request,
        'someapp/someobject_detail.html',
        {
            'object': get_object_or_404(SomeObject, slug=slug),
            'funky_page': True, # <--
        }
    )

someobject_detail.html:
<link href='{{ STATIC_URL }}css/style.css' rel='stylesheet' type='text/css'>
...
<li{% if funky_page %} class="funky"{% endif %}"></li>
...

style.css:
li.funky { background-color: red; }

If you still start with the step-by -step process of creating a Django project , some of the questions will disappear by themselves.

M
mike_k, 2012-08-03
@mike_k

Head-on method: pass a variable from view to template, by the presence of which add a css class to one or another item.
You can compare URI and named url-pattern or fill in a special block in the desired template, used in the parent for the css-class .
There is no general and suitable solution for all cases. Search for "django active link".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question