Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created August 5, 2024 00:59
Show Gist options
  • Save douglasmiranda/453e9f71901f1d4a67961f259cbc7140 to your computer and use it in GitHub Desktop.
Save douglasmiranda/453e9f71901f1d4a67961f259cbc7140 to your computer and use it in GitHub Desktop.
Django (Bootstrap) Active Menu
Sure, you could make a Template Tag. But if it's just a single main menu in the entire website.. why not just:
(Not really limited to bootstrap, just an example)
<ul class="navbar-nav">
{% with request.resolver_match.url_name as url_name %}
<li class="nav-item">
<a class="nav-link{% if url_name == "home" %} active{% endif %}" href="{% url "app:home" %}">Home</a>
</li>
<li class="nav-item">
<a class="nav-link{% if url_name == "news" %} active{% endif %}" href="{% url "app:news" %}">News</a>
</li>
{% endwith %}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment