A
A
ak2ree2020-12-30 09:40:56
Django
ak2ree, 2020-12-30 09:40:56

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>

get_category in views.py method
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)

Conclusion when searching for Building prod
Print(search_category)
BP

Print(search_item)
<QueryDict: {'search_item': ['BP']}>

Conclusion when searching for Other products
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 question

Ask a Question

731 491 924 answers to any question