简体   繁体   English

SQLAlchemy,Jinja2和Flask的键列表中的输出值

[英]Output values from list of keys with SQLAlchemy, Jinja2, and Flask

part of the html: HTML的一部分:

{% for device in casperdevices %}
        <tr>
        {% for key in casperkeys %}
            <td>
                {{ _('%(key)s', key=device[key]) }}
            </td>
            {% if key == 'Report URL' %}
            <td>
                <p><a href="{{ _('%(key)s', key=device[key]) }}">
                    {{ _('%(key)s', key=device[key]) }}</a><p>
            </td>
            {% endif %}
        {% endfor %}
        </tr>
    {% endfor %}

Model: 模型:

class Casperinv(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    serial = db.Column(db.String(64), index=True)
    display_name = db.Column(db.String(64), index=True)
    wifimac = db.Column(db.String(32), index=True)
    ip = db.Column(db.String(16), index=True)
    osvers = db.Column(db.String(16), index=True)
    username = db.Column(db.String(64), index=True)
    email = db.Column(db.String(150), index=True)

Keys= casperkeys = ['serial', 'ip', 'username'] 键= casperkeys = ['serial', 'ip', 'username']

casperdevices = Casperinv.query.all() casperdevices = Casperinv.query.all()

The page formats a column/cell as expected in the resulting page that loads, but I can't get it to actually show the output in each cell. 该页面按照加载的结果页面中的预期格式设置了列/单元格,但是我无法真正在每个单元格中显示输出。 Basically I'm trying to get a report to show in the webpage of only the columns that are selected for the logged in user's settings. 基本上,我试图使报告仅显示在登录用户设置中选择的列的网页中。

When I attempt to use variables in flask shell, it says the SQL alchemy object isn't subscriptable. 当我尝试在烧瓶外壳中使用变量时,它表示SQL炼金术对象不可下标。 I'm sure i'm missing something simple syntactically.. typing casperdevices[0].ip as an example will show an output.. but how do I get the keys to randomly output in that format to get the output i need? 我确定我在语法上缺少一些简单的东西。作为示例输入casperdevices[0].ip将显示输出。但是如何获得以该格式随机输出的键以获得我需要的输出?

Edit - I should mention that I originally tried using key=device.key above as well, with no luck. 编辑 -我应该提一下,我最初也尝试使用上面的key=device.key ,但是没有运气。

Edit 2 - I think i'm close.. if I run a loop of: 编辑2-我想我很接近..如果我运行以下循环:

for x in casperdevices:
    for y in casperkeys:
        print(vars(x)[y])

I get it to print everything I'd be looking for in the flask shell! 我得到它来打印烧瓶外壳中要查找的所有内容! However when I try to put key=vars(device)[key] in the template, it just says that vars is not defined.. cant do a `{% set x = vars(device) %} either, as that triggers the same error. 但是,当我尝试将key=vars(device)[key]放到模板中时,它只是说未定义vars。也不能执行`{%set x = vars(device)%},因为这会触发同样的错误。 :( :(

Thanks! 谢谢!

I cheated.. there's probably a better way to do this w/o changing the format.. but until someone can clue me in on that, here's a solution: Template: 我作弊..可能有一种更好的方法,无需更改格式..但在有人可以帮助我之前,这里有一个解决方案:模板:

    {% for device in casperdevices.keys() %}
        <tr>
        {% for key in casperkeys %}
            {% if key == 'reporturl' %}
            <td>
                <p><a href="{{ _('%(key)s', key=casperdevices[device][key]) }}">
                    {{ _('%(key)s', key=casperdevices[device][key]) }}</a><p>
            </td>
            {% else %}
            <td>
                {{ _('%(key)s', key=casperdevices[device][key]) }}
            </td>
            {% endif %}
        {% endfor %}
        </tr>

Routes: 路线:

@bp.route('/reports/casperinv')
@login_required
def casperinv():
    getset = Setting.query.filter_by(user_id=current_user.id).first()
    casperquery = Casperinv.query.all()
    total = len(casperquery)
    casperkeys = json.loads(getset.casper)
    casperdevices = {}
    for x in casperquery:
        casperdevices[x.serial] = vars(x)
    return render_template('/reports/casperinv.html', title=_('Casper Inventory'),
                casperdevices=casperdevices, total=total, casperkeys=casperkeys)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM