0

When a navigation link is clicked and page is redirected to that link, I am trying to highlight the active link in the nav bar. I was able to achieve the highlighting by using if and else. But I would like to know is there any way better than this. Right now I have to duplicate this code again and again.

 <li class="nav-item"> <a {% if request.path == '/register' %} class="active nav-link" {% else %} class="nav-link" {% endif %} href=" {% url 'register' %} "> Register </a> </li>

0

1 Answer 1

2

If you set the nav item URL as a variable, you can use it for the href and a check to see if it's the current page.

{% url 'about' as url %} <li class="nav-item {% if request.path == url %}active{% endif %}"> <a class="nav-link" href="{{ url }}">About</a> </li> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.