how to add an active class to current element in navbar in django

<!-- In your base template, include the following code to render the navigation links with the active class -->

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Your head content goes here -->
</head>
<body>

    <!-- Your navigation bar goes here -->
    <nav>
        <ul>
            <li class="{% if request.path == '/' %}active{% endif %}"><a href="{% url 'home' %}">Home</a></li>
            <li class="{% if request.path == '/about/' %}active{% endif %}"><a href="{% url 'about' %}">About</a></li>
            <li class="{% if request.path == '/contact/' %}active{% endif %}"><a href="{% url 'contact' %}">Contact</a></li>
            <!-- Add more navigation links as needed -->
        </ul>
    </nav>

    <!-- Your content goes here -->

</body>
</html>
# In your views.py, make sure to pass the 'request' variable to the context when rendering the template

from django.shortcuts import render

def home(request):
    # Your view logic goes here
    return render(request, 'your_template.html', {'request': request})

def about(request):
    # Your view logic goes here
    return render(request, 'your_template.html', {'request': request})

def contact(request):
    # Your view logic goes here
    return render(request, 'your_template.html', {'request': request})
/ In your CSS file, add the following style to highlight the active link in the navigation bar /

nav ul li.active {
    / Add your active styles here, for example: /
    background-color: #4CAF50;
    color: white;
}