flask:记录不会立即更新

时间:2016-04-22 16:23:40

标签: python mysql flask flask-sqlalchemy flask-admin

我的应用非常简单,其中匿名用户发布建议和主持人指定收到建议的状态(状态为:0,1,2,3)。

我正在使用Flask-Admin为主持人提供自定义列表视图(我已为每个状态添加了4个按钮)

from flask import Flask,request,url_for,redirect
from Admin.Admin import  admin,init_login
from Model import db,suggestion,User
import geoip2.webservice

app = Flask(__name__)
app.config.from_pyfile('Config.cfg')
db.init_app(app)
app.config['SQLALCHEMY_POOL_SIZE'] = 100
app.config['SQLALCHEMY_POOL_RECYCLE'] = 100



init_login(app)
admin.init_app(app)
IPclient = geoip2.webservice.Client("XXX")

def UpdateRecord(ID,status):
            try:
                    Record = suggestion.query.filter_by(id=ID).first()
                    Record.Flag=status
                    db.session.commit()
            except Exception, r:
                    raise r
                    #db.session.flush()
                    #Record = suggestion.query.filter_by(id=id).first_or_404()
                    #Record.Flag=1
                    #db.session.commit()


@app.route('/')
def index():

    return redirect("sometime ... ")



@app.route("/XXXX/Favorite/<int:id>")
def favorite(id):
    UpdateRecord(id,1)
    return redirect(url_for("admin.index")+"Favsuggestion")



@app.route("/XXXX/Archive/<int:id>")
def archive(id):
    UpdateRecord(id,2)
    return redirect(url_for("admin.index")+"Arcsuggestion")



@app.route("/WSC/Published/<int:id>")
def published(id):
    UpdateRecord(id,3)
    return redirect(url_for("admin.index")+"Pubsuggestion")


@app.route("/suggest",methods=["GET","POST"])
def suggest():
    try:
        db.session.flush()
        suggestionString=request.form.get('suggestionString')
        IpAddress=request.remote_addr
        IPresponse = IPclient.city(IpAddress)

        Country=IPresponse.country.name
        City=IPresponse.city.name
        if len(suggestionString)>0:
            suggestRecord = suggestion(suggestionString,Country,City,IpAddress)
            db.session.add(suggestRecord)
            db.session.commit()
    except Exception,e:return str(e)
    return redirect("http://site/thankyou")





@app.teardown_appcontext
def shutdown_session(response_or_exc):
    try: 
        if response_or_exc is None:
            db.session.commit()
    finally:
        db.session.remove()
    return response_or_exc

这是我的配置:

# -*- coding: utf-8 -*-
#NOTE:Replace bands with DB name
DEBUG = False
SECRET_KEY='key'
SQLALCHEMY_DATABASE_URI='mysql://user:pass@localhost/sugeestion?charset=utf8'

问题是当主持人通过点击按钮更改建议状态页面重新加载但状态在几秒钟后我没有改变我刷新然后建议状态将被更改

更新 - 2016年5月2日: 我检查了日志,发现2个与DB相关的错误

OperationalError: (_mysql_exceptions.OperationalError) (2006, 'MySQL server has gone away') [SQL: u'SELECT `User`.id AS `User_id`, `User`.`Username` AS `User_Username`, `User`.`Password` AS `User_Password`, `User`.`Permission` AS `User_Permission` \\nFROM `User` \\nWHERE `User`.id = %s'] [parameters: (1,)], referer: http://TheWebSite

StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back [SQL: u'SELECT count(%s) AS count_1 \\nFROM `suggestion` \\nWHERE `suggestion`.`Flag` = %s'] [parameters: [immutabledict({})]], referer: http://TheWebSite

1 个答案:

答案 0 :(得分:0)

好吧,

数据一致性:

我们聘请了顾问,我转而认为我们需要在每个自定义管理模型ViewView的get_query开头添加db.commit(),我们根本不怀疑。

对于错误,我们还没有解决它,我们正在玩参数但到目前为止没有任何工作。