Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-case insensitive string comparison in django template

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 %}
like image 370
priyanka priya Avatar asked Apr 28 '18 07:04

priyanka priya


1 Answers

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 %}
like image 195
art Avatar answered Sep 28 '22 10:09

art