twitter-bootstrap 在 Flask 中使用动态元素构建 Bootstrap 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32774118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:12:25  来源:igfitidea点击:

Building a Bootstrap table with dynamic elements in Flask

pythontwitter-bootstrapflasksqlalchemybootstrap-table

提问by Lucas Murtinho

I'm working with Flask on building a Bootstrap table out of a list of people taken from a SQLAlchemy database. However, the information I want to put in the table is appearing above it.

我正在与 Flask 合作,从 SQLAlchemy 数据库中提取的人员列表中构建 Bootstrap 表。但是,我想放入表格中的信息出现在它上面。

Here's the code in question:

这是有问题的代码:

<!DOCTYPE html>

{% extends "base.html" %}

{% block page_content %}
<div class="page-header">
    <h1>componentes familiares</h1>
        <table class="table">
            <thead>
                <th>name</th>
                <th>age</th>
                <th>option</th>
            </thead>
            <tbody>
                {% for person in people %}
                    <tr>{{ person.name }}</tr>
                    <tr>{{ person.age }}</tr>
                    <tr>{{ person.option }}</tr>
                {% endblock %}
            </tbody>
        </table>
{% endblock %}

(This is already a slimmed-down version, since I kept taking stuff off to see if it would solve the problem.)

(这已经是精简版了,因为我一直在拿东西看是否能解决问题。)

But let's say I have two persons in my database, Alice, and Bob. Alice is 30 and Bob is 40, Alice's option is 1 and Bob's is 2. This is what I get:

但是假设我的数据库中有两个人,Alice 和 Bob。爱丽丝 30 岁,鲍勃 40 岁,爱丽丝的选项是 1,鲍勃的选项是 2。这就是我得到的:

enter image description here

enter image description here

The information is there, but it's rendered above the table. And right below it comes the table header and an empty table row.

信息在那里,但它呈现在表格上方。在它的正下方是表格标题和一个空表格行。

Links

链接

I found another question about Bootstrap tables in Flask here, but it didn't really solve my problem. My data is being passed to the html page exactly as I want it, I just want to put it in a table.

我在这里发现了另一个关于 Flask 中 Bootstrap 表的问题,但它并没有真正解决我的问题。我的数据完全按照我的意愿传递到 html 页面,我只想将它放在一个表格中。

I also found Flask-Table, an extension to build the table in Python and then using it. It may end up being a solution, but I still don't see what's wrong with my code.

我还发现了Flask-Table,这是一个在 Python 中构建表然后使用它的扩展。它可能最终成为一个解决方案,但我仍然看不出我的代码有什么问题。

Didn't find anything useful in the Bootstrap docseither.

Bootstrap 文档中也没有找到任何有用的东西。

Any help greatly appreciated!

非常感谢任何帮助!

回答by Doobeh

You're missing a few <tr>and <td>tags:

你缺少一些<tr><td>标签:

<table class="table">
    <thead>
        <tr>
            <th>name</th>
            <th>age</th>
            <th>option</th>
        <tr>
    </thead>
    <tbody>
        {% for person in people %}
        <tr>
            <td>{{ person.name }}</td>
            <td>{{ person.age }}</td>
            <td>{{ person.option }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

You're aiming for a table-row (<tr>) per user, and some table-data (<td>) for each of their attributes. You've also got a {% endblock %}where you should have an {% endfor %}.

您的目标是为<tr>每个用户创建一个表行 ( ),并<td>为其每个属性创建一些表数据 ( )。你还有一个{% endblock %}你应该有的地方{% endfor %}