【问题标题】:Django-case insensitive string comparison in django templateDjango模板中的Django不区分大小写字符串比较
【发布时间】:2018-10-08 23:32:48
【问题描述】:

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

就我而言,当 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 %}

【问题讨论】:

标签: django django-templates wagtail


【解决方案1】:

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

<a class="{% if topic.title|lower == page.slug|lower %}menu_active{% endif %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2011-05-27
    • 2021-05-09
    • 2015-07-23
    • 2012-11-30
    相关资源
    最近更新 更多