【问题标题】:Display Query from SQLite3 Database into Bootstrap table将 SQLite3 数据库中的查询显示到 Bootstrap 表中
【发布时间】:2021-05-15 00:02:56
【问题描述】:

我想将我的查询从 SQLite3 显示到引导表中。但它似乎不起作用并且不会显示表格。 附上我的代码

views.py

@main.route('/currentlist', methods=["GET"])
def current():
    if request.method == 'GET':
        Connection = sqlite3.connect('C:\\Backup_old\\Task\\e_ticket\\my_app\\db\\customer.db')
        cursor = Connection.cursor()
        query2 = "SELECT * from customer"
        cursor.execute(query2)
        test = cursor.fetchall()
        return render_template('overview.html', testform = test)

HTML

<div class= "content">

      <table id="dtBasicExample" class="table" width="100%">
        <thead>
          <tr>
            <th class="th-sm">Date
            </th>
            <th class="th-sm">time
            <th class="th-sm">customer
            </th>
            <th class="th-sm">pic
            </th>
            <th class="th-sm">category
            </th>
            <th class="th-sm">description
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>9 December 2021</td>
            <td>15:30:00</td>
            <td>AB Corp.</td>
            <td>Bob</td>
            <td>network</td>
            <td>network is down</td>
          </tr>
          <tr>
            <td>9 December 2021</td>
            <td>17:30:00</td>
            <td>AB Corp.</td>
            <td>Alex</td>
            <td>computer</td>
            <td>computer is broken</td>
          </tr>
          <tr>
            <td>10 December 2021</td>
            <td>05:32:00</td>
            <td>CD Corp.</td>
            <td>Bob</td>
            <td>Server</td>
            <td>server is down</td>
          </tr>
          <tr>
            <td>12 December 2021</td>
            <td>10:30:00</td>
            <td>AB Corp.</td>
            <td>Bob</td>
            <td>printer</td>
            <td>printer is down</td>
          </tr>


        </tbody>
        
      </table>
    



    </div>

应该是这样,但数据来自数据库 enter image description here

我读过很多类似的问题,但大多数都使用 SQLAlchemy。

非常感谢您的帮助!

【问题讨论】:

    标签: python html sqlite flask bootstrap-table


    【解决方案1】:

    您需要对数据使用循环和模板变量。

    <tbody>
      {% for row in testform %}
      <tr>
        <td>{{ row[0] }}</td>
        <td>{{ row[1] }}</td>
        <td>{{ row[2] }}</td>
        <td>{{ row[3] }}</td>
        <td>{{ row[4] }}</td>
        <td>{{ row[5] }}</td>
      </tr>
      {% endfor %}
    </tbody>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 2014-01-15
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多