gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:30:12 25 4
gpt4 key购买 nike

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

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

  • 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 %}

    最佳答案

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

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

    关于Django 模板中不区分大小写的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50073953/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com