Highlighting Active Navigation Link - correct approach?

2,430 views
Skip to first unread message

Victor Hooi

unread,
Jan 9, 2012, 1:27:52 AM1/9/12
to django...@googlegroups.com
hi,

I have a project where I'm using Bootstrap (www.github.com/twitter/bootstrap) with Django.

I need to add a CSS class ("active") to highlight the active navigation link.

Anyhow, I did some Googling:

http://www.turnkeylinux.org/blog/django-navbar
http://ilostmynotes.blogspot.com/2010/03/django-current-active-page-highlighting.html
http://gnuvince.wordpress.com/2007/09/14/a-django-template-tag-for-the-current-active-page/
http://stackoverflow.com/questions/340888/navigation-in-django
http://djangosnippets.org/snippets/1726/
http://stackoverflow.com/questions/7665514/django-highlight-navigation-based-on-current-page

The approach in the above seems to be to either use custom template tag, middleware or JS-hackery.

Is there a current consensus in the Django community in terms of what is the "correct" approach for this?

Is there a reason something like this isn't part of Django core, or an in-built templatetag?

Cheers,
Victor

Ivo Brodien

unread,
Jan 10, 2012, 5:19:03 AM1/10/12
to django...@googlegroups.com
Hi,

strange that nobody answered yet since I guess that almost everybody encountered the same question already.

I personally did it via a custom template tag which puts out active if the current URL matches the URL pattern for a view.

Cheers
Ivo

--
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/-/Xx5Rk_rBuxIJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Matt Schinckel

unread,
Jan 10, 2012, 8:50:46 AM1/10/12
to django...@googlegroups.com
I have a reusable application I use for generating site-wide menu items. It does this (as well as dynamically showing/hiding menu items based on if the logged in user can access the view).


Actually, I think that was the one I put on pypi today, too.

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 6, 2012, 10:01:21 AM7/6/12
to django...@googlegroups.com
I've actually just done a ticket about this..


Personally, I think the approach mentioned in the ticket is a far saner way of doing things.

Cal

On Fri, Jul 6, 2012 at 12:55 PM, Dhiraj Thakur <dhira...@gmail.com> wrote:



i did this by  adding  {% block activepage %}{% endblock %} in my base template and in a page which extended from the basic template i added {% block activepage %} class="active" {% endblock %}  where active class does the highlight in CSS.

I think JS/jQuery is neat but if you are not comfortable with it i think templates do the trick.. :)

 

--
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/-/9m0Ss0Od_LUJ.

Melvyn Sopacua

unread,
Jul 6, 2012, 10:20:02 AM7/6/12
to django...@googlegroups.com
On 6-7-2012 16:01, Cal Leeming [Simplicity Media Ltd] wrote:
> I've actually just done a ticket about this..
>
> https://code.djangoproject.com/ticket/18584
>
> Personally, I think the approach mentioned in the ticket is a far saner way
> of doing things.

Just wondering about this:
if res:
return {'urlname' : res.url_name}

a) no else clause, so there's a codepath where no dict is returned
b) if that can't be reached, then why the if statement.

Other that that, it probably needs a bit of refinement if you're going
to force a naming convention, then force the convention on how to deal
with sub menus, so that we can generate stuff like:
News
| world
>> local
| business
Polls
| current
| recent
etc and have both "news" and "local" highlighted. For example it makes
sense for urlnames like this to have a double underscore separator, so
the urlname for news/local would be news__local.
Then again, it might be overkill.
--
Melvyn Sopacua


Cal Leeming [Simplicity Media Ltd]

unread,
Jul 6, 2012, 10:25:52 AM7/6/12
to django...@googlegroups.com
You raise a good point about sub menus.

Possibly something like this:

def resolve_urlname(request):
    """Allows us to see what the matched urlname for this
    request is within the template"""
    from django.core.urlresolvers import resolve
    try:
        res = resolve(request.path)

        if res:
            return {
                'urlname' : res.url_name,
                'urlparts' : res.url_name.split("__")
            }
    except:
        return {}

But then that forces a naming convention which might not be desired.

The other option is to see if the core devs would accept a patch which allows you to specify a 'urlname group' on the url() method in urls.py..

Cal

--
You received this message because you are subscribed to the Google Groups "Django users" group.
Reply all
Reply to author
Forward
0 new messages