Answer the question
In order to leave comments, you need to log in
Highlight active navbar navbar in BOOTSTRAPv.4 + Django?
I want to highlight the active link of the navbar by category. It seems that everything was written correctly, but in fact, when you go to one of the links, both are highlighted. I tried many options.. I don't want to create a separate method for other products)))
search_results.html
<li class="nav-item {% if search_category == BP%}active{% endif%}">
<a class="nav-link" href="{% url 'core:get_category' %}?search_item=BP">Building Products</a>
</li>
<li class="nav-item {% if search_category == BP%}active{% endif%}">
<a class="nav-link" href="{% url 'core:get_category' %}?search_item=OT">Other Products</a>
</li>
def get_category(request):
queryset = Item.objects.all()
search_category = request.GET.get('search_item','')
if search_category:
queryset = queryset.filter(category__icontains = search_category)
context ={
'queryset': queryset
}
return render(request, 'search_results.html', context)
Print(search_category)
BP
Print(search_item)
<QueryDict: {'search_item': ['BP']}>
Print(search_category)
OT
Print(search_item)
<QueryDict: {'search_item': ['OT']}>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question