Reputation: 6544
I have the view:
def about(request):
return render(request, 'about.html', {'page_about_exist':1,})
In html I have this code:
<li {% if page_about_exist %} class="active" {% endif %}><a href="{% url about_project %}"> About</a></li>
So, If I go to the About-page, in menu it has class active, and I can see it visually. Is there any other way without the dictionary? Because I have in url
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login',),
and my way with dictionary doesnt look great there. Thanks.
Upvotes: 1
Views: 281
Reputation: 2017
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.
Upvotes: 1
Reputation: 7487
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.
Upvotes: 1