猿问

Python数据到html表的for循环

我正在编写一个 Python 脚本,它使用 sqlite3 从数据库中读取记录,然后需要将其显示在 HTML 网页上的表格中。我似乎不明白我应该如何将数据从 python 发送到 HTML 代码,然后在 HTML for 循环中使用它,所以表与数据库中的记录量一样长。我正在使用 Python Flask 来完成此操作。任何帮助,将不胜感激!


表格示例:


<table>

    <tr>

        <th>Column 1</th>

        <th>Column 2</th>

    </tr>

    <tr>

        <td>some data 1.1</td>

        <td>some data 1.2</td>

    </tr>

    <tr>

        <td>some data 2.1</td>

        <td>some data 2.2</td>

    </tr>

</table>


慕斯王
浏览 162回答 1
1回答

jeck猫

from flask import render_templateimport flaskyourdb=[&nbsp; &nbsp; &nbsp;("your data 1.1","your data 1.2"),&nbsp; &nbsp; &nbsp;("your data 2.1","your data 2.2"),&nbsp; &nbsp; &nbsp;("your data 3.1", "your data 3.2")&nbsp; &nbsp; &nbsp;]@app.route("/")def main():&nbsp; &nbsp; return render_template("index.html",data=yourdb)和html页面<html><head>&nbsp; &nbsp; <title>&nbsp; &nbsp; &nbsp; &nbsp; İndex&nbsp; &nbsp; </title></head><body><table>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Your column Name 1&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Your Column Name 2&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; {% for x in data %}&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{x[0]}}&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{x[1]}}&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; {% endfor %}</table></body></html>文件夹结构--templates|&nbsp; &nbsp;|__index.html|--flashapp.py
随时随地看视频慕课网APP

相关分类

Python
我要回答