Jinja2模板无法正确呈现if-elif-else语句 - python

我正在尝试在Jinja2模板中使用CSS设置文本颜色。在下面的代码中,如果变量包含字符串,我想将输出字符串设置为以特定的字体颜色打印。每次生成模板时,尽管由于else语句而将其打印为红色,但即使输出应该匹配,它也始终不会看到前两个条件,我可以说出该表生成时变量的输出是什么,并且符合预期。我知道我的CSS是正确的,因为默认情况下该字符串以红色打印。

我的第一个想法是将要检查的字符串括在引号中,但这没有用。接下来是jinja没有扩展RepoOutput[RepoName.index(repo)],但是它上面的for循环起作用了,RepoName正确地扩展了。我知道如果添加花括号,它将打印出我肯定会破坏模板或无法正常工作的变量。

我尝试查看了这些站点,并遍历了全局表达式的列表,但是找不到与我的示例类似的示例或进一步寻找方向的示例。

http://jinja.pocoo.org/docs/templates/#if

http://wsgiarea.pocoo.org/jinja/docs/conditions.html

   {% for repo in RepoName %}
       <tr>
          <td> <a href="http://mongit201.be.monster.com/icinga/{{ repo }}">{{ repo }}</a> </td>
       {% if error in RepoOutput[RepoName.index(repo)] %}
          <td id=error> {{ RepoOutput[RepoName.index(repo)] }} </td> <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
       {% elif Already in RepoOutput[RepoName.index(repo) %}
          <td id=good> {{ RepoOutput[RepoName.index(repo)] }} </td>   <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
       {% else %}
            <td id=error> {{ RepoOutput[RepoName.index(repo)] }} </td> <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
       </tr>

       {% endif %}
   {% endfor %}

谢谢

参考方案

您正在测试error中是否存在变量AlreadyRepoOutput[RepoName.index(repo)]的值。如果这些变量不存在,则使用undefined object。

因此,您的ifelif测试都为假; RepoOutput [RepoName.index(repo)]的值中没有未定义的对象。

我认为您想测试某些字符串是否在值中:

{% if "error" in RepoOutput[RepoName.index(repo)] %}
    <td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% elif "Already" in RepoOutput[RepoName.index(repo) %}
    <td id="good"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% else %}
    <td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% endif %}
</tr>

我所做的其他更正:

  • 使用{% elif ... %}代替{$ elif ... %}
  • </tr>标记移出了if条件结构,它必须始终存在。
  • id属性
  • 周围加上引号

    请注意,很可能您想在此处使用class属性,而不是id,后者必须具有在整个HTML文档中唯一的值。

    就个人而言,我将在此处设置类的值,并减少一些重复:

    {% if "Already" in RepoOutput[RepoName.index(repo)] %}
        {% set row_class = "good" %}
    {% else %}
        {% set row_class = "error" %}
    {% endif %}
    <td class="{{ row_class }}"> {{ RepoOutput[RepoName.index(repo)] }} </td>
    

    如何使用BeautifulSoup在<tr>中捕获特定的<td> - python

    尝试从nyc Wiki页面中的高中列表中获取所有高中名称。我已经写了足够多的脚本,可以让我获取包含在高中,学业和入学条件列表的表的<tr>标记中的所有信息-但是我如何才能缩小到我认为的范围内在td[0]内休息(会弹出KeyError)-只是学校的名称?到目前为止我写的代码:from bs4 import BeautifulSoup from ur…

    Python numpy数据指针地址无需更改即可更改 - python

    编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …

    Python Pandas导出数据 - python

    我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

    Python:对于长时间运行的进程,通过还是休眠? - python

    我正在编写一个队列处理应用程序,该应用程序使用线程等待和响应要发送到该应用程序的队列消息。对于应用程序的主要部分,只需要保持活动状态即可。对于像这样的代码示例:而True: 通过要么而True: time.sleep(1)哪一个对系统的影响最小?除了保持python应用运行外,什么都不做的首选方式是什么? 参考方案 我可以想象time.sleep()会减少系…

    Python:无符号32位按位算术 - python

    试图回答另一篇有关其解决方案涉及IP地址和网络掩码的文章时,我陷入了普通的按位算法。在Python中,是否存在一种标准的方式来进行按位AND,OR,XOR,NOT运算,假设输入是“32位”(可能是负数)整数或long,并且结果必须是[[ 0,2 ** 32]?换句话说,我需要一个与无符号长整数之间的C按位运算有效的Python对应物。编辑:具体问题是这样的:…