'Dynamic inline CSS styling in Django template
I am new to Django and am trying to figure out how to use dynamic CSS that is only active for the current category a user is on.
I have a side nav with some categories and whichever category the user is on should be active with the use of a class.
This is currently how I have things:
{% for category in categories %}
<li><a href="{% url 'category' category.id %}"
{% if category.id in request.path %}
class="uk-text-bold"
{% endif %} >{{ category.name }}</a></li>
{% endif %}
This is obviously not correct and doesn't work so I imagine there is a proper way to do something like this and I'm just having a hard time understanding or finding that out.
Any help is appreciated! Thanks.
Solution 1:[1]
The second {% endif %} should be changed for an {% endfor %}. Then everything works the way you expect. I do this in my code all the time and I've had no problems with it.
{% for category in categories %}
<li>
<a href="{% url 'category' category.id %}"
{% if category.id in request.path %} class="uk-text-bold" {% endif %} >
{{ category.name }}
</a>
</li>
{% endfor %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | saarrooiii |
