I have a dynamic category menu list in my template that I would like to highlight (set active) when the url parameter matches the menu item. Therefore when the page equals the menu item the category will be highlighted.
URL examples:
https://[domainname]/training/?category=1
https://[domainname]/training/?category=2
https://[domainname]/training/?category=3
{% for categories in category_list %}
<a href="{% url 'training' %}?category={{categories.Category_id}}" >
{% endfor%}
CodePudding user response:
You should move your if block before displaying the a tag, add active class only when your if condition is fulfilled, like that:
{% for categories in category_list %}
{% if request.GET.category == 'categories.Category_id' %}
<a href="{% url 'training' %}?category={{categories.Category_id}}" >
{% else %}
<a href="{% url 'training' %}?category={{categories.Category_id}}" >
{% endif %}
{% endfor%}
Just verify the condition if it's really select the case you want or not.