sqlite数据库更新时自动更新烧瓶网页

时间:2016-09-17 15:43:12

标签: sqlite python-3.x flask

以下代码正在运行,但为了获得新城市,用户需要刷新页面。我想要的是当数据库更新时,它应该反映在网页上而无需用户刷新页面。

我测试了 TEMPLATES_AUTO_RELOAD = True ,但我没有让它工作。

from flask import Flask, render_template, g
import sqlite3
DATABASE = 'new.db'

app = Flask(__name__)
app.config.from_object(__name__)

def connect_db():
   return sqlite3.connect(app.config['DATABASE'])

@app.route('/')
def main():
   g.db = connect_db()
   cur = g.db.execute('select * from pop')
   post = [dict(city=row[0], population=row[1]) for row in cur.fetchall()]
   g.db.close()
   return render_template('index.html')

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

index.html 看起来:

<html>
 <body>
  <h3>Reading from sqlite</h3>
  {% for p in posts %}
   <stron>city</strong> {{p.city}}<br>
  {% endfor%}
 </body>
</html>

0 个答案:

没有答案