【问题标题】:Change color is table cell based of columns in database更改颜色是基于数据库中列的表格单元格
【发布时间】:2020-11-29 16:27:37
【问题描述】:

所以在我永无止境地学习编写 python 代码的过程中,我有一张从 Jinja2 构建的 IP 地址表。如果它是我的 Postgres 数据库中的 IP 地址,我正在尝试找出一种方法来获取此表并更改单元格的颜色。我在这个项目中使用了 Flask 和 flask_sqlalchemy。

    class Gear(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)
    netname = db.Column(db.String())
    ipaddress = db.Column(db.String())
    subnet = db.Column(db.String())

    def __repr__(self):
        return '<gear %r>' % self.id
<div class="container">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th scope="col">2.0.0.X</th>
                <th scope="col">2.0.1.X</th>
                <th scope="col">200.0.0.X</th>
                <th scope="col">200.0.1.X</th>
                <th scope="col">192.168.0.X</th>
                <th scope="col">192.168.1.X</th>
                <th scope="col">10.0.0.X</th>
                <th scope="col">10.0.1.X</th>
            </tr>
        </thead>
        <tbody>
            {% for n in range(1, 255) %}
            <tr>
                <td>2.0.0.{{n}}</td>
                <td>2.0.1.{{n}}</td>
                <td>200.0.0.{{n}}</td>
                <td>200.0.1.{{n}}</td>
                <td>192.168.0.{{n}}</td>
                <td>192.168.1.{{n}}</td>
                <td>10.0.0.{{n}}</td>
                <td>10.0.1.{{n}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

【问题讨论】:

    标签: python flask bootstrap-4 jinja2 flask-sqlalchemy


    【解决方案1】:

    试试:

    views.py:

    @app.route('/our_ips')
    def our_ips():
        #                  whatever query format you use
        our_ip_addresses = db.session.execute("select ipaddress from Gear") 
        # you want something like: 
        # our_ip_addresses = ['2.0.0.10', '2.0.0.50', '2.0.0.100']
    
        return render_template("our_ips.html", our_ips=our_ip_addresses)
    

    然后,在our_ips.html

    <table>
        {% for n in range(1, 255) %}
            <tr>
            {% if ("2.0.0." + n|string) in our_ips %}
                <td class="greenText">
                     2.0.0.{{n}}
                </td>
            {% else %}
                <td>
                    2.0.0.{{n}}
                </td>
            {% endif %}
                
            </tr>
        {% endfor %}
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2015-01-24
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      相关资源
      最近更新 更多