Html建立不同長度的表(在flask中首選)
P粉133321839
P粉133321839 2023-08-01 20:40:21
0
1
564
<p>我想從資料庫中獲取數據,並將其匯入到網站表中。假設sql資料庫中有50行。那麼表中一定有50行。但是,當我嘗試建立一個表,我必須透過手動新增每個行和顏色的標籤。那麼我應該添加幾十個聲音行,並通過控制它們的可見性來實現這一點嗎?我目前使用燒瓶,但如果沒有辦法實現它的燒瓶,其他方式也被接受。 </p>
P粉133321839
P粉133321839

全部回覆(1)
P粉401901266

在Flask中,您可以根據SQL資料庫中的資料動態產生具有不同長度的HTML表。您不需要手動建立數千行並控制其可見性。相反,您可以使用整合到Flask中的模板引擎來輕鬆實現這一點。希望這對你有幫助

從SQL資料庫檢索資料:使用Flask的資料庫整合從資料庫取得資料。

將資料傳遞給模板:在您的Flask路由中,將從資料庫檢索到的資料作為變數傳遞給HTML模板。

使用模板:在HTML模板中,使用語法遍歷資料並動態產生表格行和單元格。


from flask import Flask, render_template

app = Flask(__name__)

# Replace this with your database connection and query code to fetch data
# For demonstration purposes, let's assume you have fetched data in the 'rows' variable
rows = [
    {'id': 1, 'name': 'John', 'age': 25},
    {'id': 2, 'name': 'Jane', 'age': 30},
    # Add more rows as needed
]

@app.route('/')
def index():
    return render_template('table_template.html', rows=rows)

if __name__ == '__main__':
    app.run(debug=True)

HTML FILE

<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Table</title>
    <style>
        /* Add border to the table */
        table {
            border-collapse: collapse;
            width: 100%;
            border: 1px solid black;
        }

        /* Add bold font style to the header row */
        th {
            font-weight: bold;
        }

        /* Add border to table cells (optional) */
        td, th {
            border: 1px solid black;
            padding: 8px;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Age</th>
            </tr>
        </thead>
        <tbody>
            {% for row in rows %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.name }}</td>
                    <td>{{ row.age }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板