Home > Mobile >  What is the best practice for having html tags change their style after changing URL in Django?
What is the best practice for having html tags change their style after changing URL in Django?

Time:09-22

enter image description here

Suppose I have this kind of navbar, the buttons in which turn white when you click it (adding an "active" class). But if the button redirects to a new url, the navbar renders anew, and the home icon is highlighted as it is by default. How to drag that "active" class on a button after a redirect? What is the best practice in that regard? Do I ask the wrong question?

CodePudding user response:

You can do something like:

views.py

def render_about(request):

    # include title in context
    context = {
        'title' : 'About'
    }

    return render(request, 'about.html', context=context)

Then in your navbar:

base.html

<nav>
  <a class="{% if title=='Home' %}active{% endif %}"> Home </a>
  <a class="{% if title=='About' %}active{% endif %}"> About </a>
  ...
</nav>

And then add the styling for the active class in your css file.

  • Related