猿问

如果满足条件,则将图形添加到“数据表”单元格

我正在使用python / flask + flask-sqlalchemy从MySQL数据库中提取数据,并将其显示在html中。


“ t.status ”的值只能为0,1或2。


这是我的html代码的外观:


<tbody>

    {% for t in tdata %}

        <tr>

            <td>{{ t.id }}</td>

            <td>{{ t.objectid }}</td>

            <td>{{ t.status }}</td>

        </tr>

    {% endfor %}

</tbody>

运行正常,并显示所有状态代码。现在,我需要为所有3种可能的状态代码添加微小的颜色图标。我尝试了以下代码,但是没有用:


<tbody>

    {% for t in tdata %}

        <tr>

            <td>{{ t.id }}</td>

            <td>{{ t.objectid }}</td>

            <td>{{ if t.status == '0':

                       <img src="/static/images/red.png"> + t.status

                   elif t.status == '1':

                       <img src="/static/images/green.png"> + t.status

                   else:

                       <img src="/static/images/yellow.png"> + t.status }}</td>

        </tr>

    {% endfor %}

</tbody>

感谢您的任何帮助。


慕虎7371278
浏览 142回答 2
2回答

弑天下

添加图标标签与添加img标签相同。<tbody>&nbsp; &nbsp; {% for t in tdata %}&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ t.id }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ t.objectid }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{{ if t.status == '0':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-cloud"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="/static/images/red.png"> + t.status&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif t.status == '1':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="/static/images/green.png"> + t.status&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="/static/images/yellow.png" + t.status }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; {% endfor %}</tbody>

慕的地6264312

我想到了。{{需要更改为{%,并且每一行都必须用%}结束。省略号后没有':'。<tbody>{% for t in tdata %}&nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{ t.id }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{ t.objectid }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{% if t.status == 0 %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="red"> ONE </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% elif t.status == 1 %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="green"> TWO </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% elif t.status == 2 %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="yellow"> THREE </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% endif %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp; </tr>{% endfor %}</tbody>
随时随地看视频慕课网APP

相关分类

Python
我要回答