Django-case insensitive string comparison in django template
Asked Answered
J

1

6

How to do case insensitive string comparison?

In my case , i need to add a class menu_active when topic.title equals page.slug. but,now

  • topic.title= home
  • page.slug = Home

so my condition fails

nav_bar.html

{% for topic in landing_pages %}
     <li role="presentation">
<a class="{% if topic.title == page.slug %}menu_active{% endif %}" href="/{{topic.slug}}/">{{topic.title}}</a>
     </li>
{% endfor %}
Jehol answered 28/4, 2018 at 7:17 Comment(1)
Possible duplicate of Django Model - Case-insensitive Query / FilteringCivil
B
10

Pass the strings through the built-in template tag lower/upper and then compare.

<a class="{% if topic.title|lower == page.slug|lower %}menu_active{% endif %}
Beal answered 28/4, 2018 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.