[Answered ]-Menu navigation with Django

1๐Ÿ‘

โœ…

You should do this kind of thing with template inheritance. For example, your base.html might include a navigation list with:

<li{% block products %}{% endblock %}><a href...>Products</a></li>
<li{% block about %}{% endblock %}><a href...>About</a></li>

Then in your about template (assuming it inherits from base) you have:

{% block about %} class="active"{% endblock %}

This will render as pure html, using the class you have defined for active pages. Because it uses simple template inheritance you can also get really fine control with this.

๐Ÿ‘คcms_mgr

1๐Ÿ‘

I use my own template tag called ifnav.

The usage is very simple. Just add it to your INSTALLED_APPS and make sure the request context processor is activated.

Afterwards you can write this:

<li {% ifnav "^/about/" %} class="active" {% endifnav %}><a href="{% url about_project %}"> About</a></li>

For current projects I use Django CMS which takes care of rendering the navigation.

๐Ÿ‘คbikeshedder

Leave a comment