51π
This solution didnβt work when the href attribute of your links will be different from href="#"
. Why ? Simply because each click on a link triggers a request to the server. In your JS code, you add the method preventDefault() in order to avoid this basic behavior, but I think that not really your objective for this purpose, isnβt it ?
To handle this kind of feature you can update your template code by adding something like this :
base.html
<div class="collapse navbar-collapse" id="tn-navbar-collapse">
<ul class="nav navbar-nav">
<li class="{% if nbar == 'home' %}active{% endif %}">
<a href="/">HOME</a>
</li>
...
</ul>
</div>
views.py
def your_view(request):
...
return render(request, 'yourtemplate.html', {'nbar': 'home'})
With this way, you donβt have to manage this feature with javascript.
56π
If you dont want to send any additional context from views then you can handle it like this with resolver_match:
<li {% if request.resolver_match.url_name == 'home' %}class="active"{% endif %}>
<a href="/">HOME</a>
</li>
where βhomeβ is the name given to your url pattern for / (probably '^$'
) in your urls.py.
So obviously to use this you need to name all your url patterns, which is a good practice anyway.
- [Django]-405 "Method POST is not allowed" in Django REST framework
- [Django]-Django: change the value of a field for all objects in a queryset
- [Django]-Django Queryset with filtering on reverse foreign key
8π
Alternative including URL namespaces:
<li class="nav-item {% if request.resolver_match.view_name == 'scores:list' %}active{% endif %}">
<a class="nav-link" href="{% url 'scores:list' %}">Scores</a>
</li>
- [Django]-How to redirect with post data (Django)
- [Django]-Conversion of datetime Field to string in django queryset.values_list()
- [Django]-Django URLS, how to map root to app?
1π
I recently ran into this problem and tried every solution I could find. Eventually, I came to this. This example assumes the path to this content is yoursite.com/about
Give your link an id.
<li><a id="about" href="#">About</a></li>
var path = $(location).attr('pathname')
var pa = path.split("/")
$('#'+pa[1]).tab('show')
- [Django]-Link in django admin to foreign key object
- [Django]-Django project root self discovery
- [Django]-Django storages: Import Error β no module named storages
1π
@rphonika has give a nice answer. But if you have many navigation menus then adding template logic in each list item makes the code untidy. Menu items can be assigned a unique id, the id passed in views and active class can be assigned using a one liner javascript code in html itself.
base.html
<ul class="nav navbar-nav navbar-left">
<li><a class="navmenu-a" id="overview" href="{% url 'webapp:overview' %}">Overview</a></li>
<li><a class="navmenu-a" id="plot" href="{% url 'webapp:plot' %}">Plot</a></li>
<li><a class="navmenu-a" id="board" href="{% url 'webapp:board' %}">Board</a></li>
<li><a class="navmenu-a" id="storyboard" href="{% url 'webapp:storyboard' %}">Storyboard</a></li>
<li><a class="navmenu-a" id="freqs" href="{% url 'webapp:freqs' %}">Faq</a></li>
</ul>
.
.
.
<script type="text/javascript">
{% if nmenu %} $("a#{{nmenu}}").addClass("navmenu-a-active") {% endif %}
</script>
views.py
def overview(request):
...
return render(request, 'overview.html', {'nmenu': 'overview'})
def plot(request):
...
return render(request, 'plot.html', {'nmenu': 'plot'})
And so on for other views
- [Django]-Django F() division β How to avoid rounding off
- [Django]-How to migrate back from initial migration in Django 1.7?
- [Django]-Get Model instance from Form without saving
1π
I would rather prefer to only edit the template using rather than editing both views and templates for this. People needing the same, rather than following the accepted answer, may consider the below way:
<ul class="navbar-nav">
<li class="nav-item {% ifequal request.path '/' %} active {% endifequal %}">
<a class="nav-link" href="/">Home</a>
</li>
</ul
You can change βindexβ above with your own
- [Django]-Python + Django page redirect
- [Django]-Separating form input and model validation in Django?
- [Django]-Django β run a function every x seconds
1π
I think the easiest solution is JavaScript / CSS:
Anyway, here is the solution using Jquery. Notice n1,n2
classes in html
In base.html
<nav>
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link n1" href="{% url 'polls:dashboard' %}">
<span data-feather="home"></span>
Dashboard
</a>
</li>
<li class="nav-item">
<a class="nav-link n2" href="{% url 'polls:restaurants' %}">
<span data-feather="file"></span>
Restaurants
</a>
</li>
</ul>
</nav>
<script>
$('nav').find('{% block active_link%}{%endblock%}').addClass('active')
</script>
Then in your dashboard template:
{% block active_link %}.n1{%endblock%}
In your restaurants template:
{% block active_link %}.n2{%endblock%}
- [Django]-Python Django Global Variables
- [Django]-Error: No module named psycopg2.extensions
- [Django]-Django queries: how to filter objects to exclude id which is in a list?
1π
The most Django-canonical way of adding an active
class to the nav item for the current page is with template inheritance. No JavaScript required. In your base template(ex: base.html
) add a block for each nav link active class like this:
<ul class="nav navbar-nav">
<li class="{% block nav_home_class %}{% endblock %}"><a href="#">Home</a></li>
<li class="{% block nav_about_class %}{% endblock %}"><a href="#">About</a></li>
<li class="{% block nav_contact_class %}{% endblock %}"><a href="#">Contact</a></li>
...
Then in each pageβs template, set the matching nav_*_class
block to active
. For home.html
:
{% extends "base.html" %}
{% block nav_home_class %}active{% endblock %}
...
The resulting html in the home page will look like:
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li class=""><a href="#">About</a></li>
<li class=""><a href="#">Contact</a></li>
...
- [Django]-Django Framework β Is there a shutdown event that can be subscribed to?
- [Django]-How to subquery in queryset in django?
- [Django]-AssertionError: database connection isn't set to UTC
0π
After adding gamerβs JavaScript into your template code_here , you got to make sure you have the similar settings in your CSS file also, in order to make JavaScript work.
.navbar-nav > li.active > a {
color: #d74b4b;
background: transparent;
border-bottom: none;
}
- [Django]-Using django-admin on windows powershell
- [Django]-Turn off caching of static files in Django development server
- [Django]-Django admin login suddenly demanding csrf token
0π
You can check if a string is in your URL path. This is helpful for hierarchical or nested URL paths
<li {% if 'home' in request.get_full_path %}class="active"{% endif %}>
<a href="/">HOME</a>
</li>
- [Django]-XlsxWriter object save as http response to create download in Django
- [Django]-"Post Image data using POSTMAN"
- [Django]-Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?
0π
def my_view(request):
active_tab = 'home' # set the active tab here
context = {'active_tab': active_tab}
return render(request, 'my_template.html', context)
Then, in your template, you can use an if statement to check if the current page matches the active tab and apply a CSS class to the active tab.
<ul class="nav nav-tabs">
<li {% if active_tab == 'home' %}class="active"{% endif %}>
<a href="{% url 'home' %}">Home</a>
</li>
<li {% if active_tab == 'about' %}class="active"{% endif %}>
<a href="{% url 'about' %}">About</a>
</li>
<li {% if active_tab == 'contact' %}class="active"{% endif %}>
<a href="{% url 'contact' %}">Contact</a>
</li>
</ul>
- [Django]-TextField missing in django.forms
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-How should I write tests for Forms in Django?
-2π
If you want activate by context you can do like this variable == to context in .views
<li class="nav-item {% ifequal variable context %}active{% endifequal%}">
<a class="nav-link" href="{% url 'scores:list' %}">Scores</a>
</li>
- [Django]-ValueError β Cannot assign: must be an instance
- [Django]-Django form fails validation on a unique field
- [Django]-Redis Python β how to delete all keys according to a specific pattern In python, without python iterating