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 %}
priyanka priya
  • 165
  • 2
  • 9
  • Possible duplicate of [Django Model - Case-insensitive Query / Filtering](https://stackoverflow.com/questions/11743207/django-model-case-insensitive-query-filtering) – Manmohan_singh Apr 28 '18 at 07:20

1 Answers1

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 %}
art
  • 1,358
  • 1
  • 10
  • 24