首页 > 解决方案 > 使用 jinja 标签在 html 标签内使用 if 条件

问题描述

我为我的站点项目制作了一个标题,它有一些部分......我想使用 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>

标签: pythonhtmldjangopython-3.xjinja2

解决方案


<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>

推荐阅读