在Python Flask中刷新网页时重新加载数据

时间:2017-10-19 07:09:10

标签: html mysql python-3.x flask

嗨,我知道我的英语不好,但我希望你理解它。

我使用flask在python中创建了一个Web服务器。

我从数据库导入数据,然后将数据发送到html进行操作

它工作正常,但我希望每次单击刷新时都会更新数据

这是一个python代码

from flask import Flask, render_template, request, redirect,url_for
import MySQLdb

# modify (mysql url, mysql id , mysql password, db name)

db = MySQLdb.connect("localhost","root", "4662", "nomore")
cursor = db.cursor()

# modify webpage flask

app=Flask(__name__)

@app.route('/admin',methods=['GET'])
def admin():
    if request.method=='GET':
        cursor.execute("SELECT gx FROM satcandata ORDER BY id DESC LIMIT 1")
        gx = cursor.fetchone()
        return render_template("admin.html",gx0=gx)


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

和html代码

<html lang="ko">
    <head>
        <meta charset="utf-8">
        <title>test homepage</title>
        <style>
        table{width:100%;}
        table,th,td{border: 1px solid #bcbcbc;}
        </style>
    </head>
    <body>
        <table>
            <caption>satcan data</caption>
        <thead>
            <tr>
                <th>num</th>
                <th>gx</th>

            </tr>
        </thead>
        <tbody>

                <th>num0:</th>
                <td>{{gx0}}</td>

            </tr>
        </tbody>
        <tfoot>
            <tr></tr>
        </tfoot>
    </table>
    </body>
</html>

如果我必须更改方法,我应该将其更改为什么?

或者,如果你有其他方法,请教我。

0 个答案:

没有答案