【问题标题】:use if condition inside html tag using jinja tag使用 jinja 标签在 html 标签内使用 if 条件
【发布时间】:2019-11-19 03:35:00
【问题描述】:

我为我的网站项目做了一个标题,它有一些部分...... 我想使用 jinja 更改标题上的部分颜色名称,但我不能 模板已无错误地呈现,但部分名称未更改 并且在 pycharm 中显示此消息:标签开始未关闭

<ul class='menu'>
    <li {% if section == "dashboard" %}class="selected"{% endif %}>
      <a href="{% url 'dashboard' %}">My dashboard</a>
    </li>
    <li {% if section == "images" %}class="selected"{% endif %}>
      <a href="#">Images</a>
    </li>
    <li {% if section == "people" %}class="selected"{% endif %}>
      <a href="#">People</a>
    </li>
</ul>

【问题讨论】:

  • 你能在你的视图中显示print((section, type(section))) 显示的内容吗?或许section不是字符串,或者是不同值的字符串?

标签: python html django python-3.x jinja2


【解决方案1】:

试试这个

{% if section == "dashboard" %}
<li class="selected">
{% else %}
<li>
{% endif %}

【讨论】:

    【解决方案2】:
    <ul class='menu'>
        <li class="selected">
            {% if section == "dashboard" %}
              <a href="{% url 'dashboard' %}">My dashboard</a>
            {% elif section == "images" %}
              <a href="#">Images</a>
            {% elif section == "people" %}
              <a href="#">People</a>
            {% endif %}
         </li>
     </ul>
    

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 1970-01-01
      • 2017-08-12
      • 2017-06-07
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2023-03-24
      相关资源
      最近更新 更多