django how to set a navbar active

Solutions on MaxInterview for django how to set a navbar active by the best coders in the world

showing results for - "django how to set a navbar active"
Dexter
16 Jan 2019
1# navbar_demo/pages/views.py... 
2
3'''We will pass a unique variable for each page whose value will be 'active' to make 
4a page active in navbar when it call.'''
5
6def index(request):    
7  context = {"home_page": "active"} # new info here    
8  return render(request, 'pages/index.html', context)
9
10def about(request):    
11  context = {"about_page": "active"} # new info here    
12  return render(request, 'pages/about.html', context)
13
14def contact(request):    
15  context = {"contact_page": "active"} # new info here    
16  return render(request, 'pages/contact.html', context)
17
18# In html file #
19 
20'''  
21<ul class="navbar-nav">
22  <li class="nav-item {{ home_page }}">
23      <a class="nav-link" href="{% url 'index' %}">Home</a>
24   </li>
25   <li class="nav-item {{ about_page }}">
26      <a class="nav-link" href="{% url 'about' %}">About</a>
27    </li>
28   <li class="nav-item {{ contact_page }}">
29       <a class="nav-link" href="{% url 'contact' %}">Contact</a>
30    </li>
31 </ul>
32'''
Veronica
04 Jun 2016
1<li class="nav-item {% if request.resolver_match.view_name == 'scores:list' %}active{% endif %}">
2    <a class="nav-link" href="{% url 'scores:list' %}">Scores</a>
3</li>
4