Django 模板中不区分大小写的字符串比较

pri*_*iya 5 django django-templates wagtail

如何进行不区分大小写的字符串比较?

就我而言,当 topic.title 等于 page.slug 时,我需要添加一个menu_active类。但现在

  • topic.title=家
  • page.slug = 主页

所以我的条件失败了

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 %}
Run Code Online (Sandbox Code Playgroud)

art*_*t06 8

通过内置模板标签lower/upper 传递字符串,然后进行比较。

<a class="{% if topic.title|lower == page.slug|lower %}menu_active{% endif %}
Run Code Online (Sandbox Code Playgroud)