This is a sample of my current list-group:
Since Python Flow Control
link on side bar is active, I want it to be highlighted by adding a CSS active
class.
I think I can do that using current URL of the page, being in Python Flow Control
page the current URL looks like http://localhost:8000/python/python-flow-control/
and in template if I type {{ request.path }}
it would return /python/python-flow-control/
.
Using URL I tried this approach but it didn't work:
<div class="list-group">
{% for sub_cat in sub_cats %}
<a href="{% url 'tutorials:tutorials' sub_cat.sub_cat_parent.cat_slug sub_cat.sub_cat_slug %}" class="list-group-item list-group-item-action {% if request.path == '/{{sub_cat.sub_cat_parent.cat_slug}}/{{sub_cat.sub_cat_slug}}/' %} active {% endif %}">
{{ sub_cat.sub_cat_title }}</a>
{% endfor %}
</div>
I added {% if request.path == '/{{sub_cat.sub_cat_parent.cat_slug}}/{{sub_cat.sub_cat_slug}}/' %} active {% endif %}
to check if the current URL matches with url of the current clicked link and if it matches add css active
class.
However, this had no effect. It didn't throw any error and didn't work either.
edit: I tried this, it didn't work either
{% url '{{request.path}}' category='python' sub_cat='python-introduction' as target %}
{% if target %}
<div class="bg-secondary">active</div>
{% endif %}