python - flask返回值到页面问题
PHP中文网
PHP中文网 2017-04-17 17:29:37
0
1
291

1、定义的路由

@main.route('/qqsql',methods=['GET','POST'])
def qqsql():
    cmdsql = None

    commandform = CommandForm()
    serverlists = GameServer.query.all()
    if commandform.validate_on_submit():
        # global dbname
        session['cmdsql'] = commandform.command.data
        session['serverID'] = request.form.getlist('dbcheckbox')
        for db in session.get('serverID'):
            dbsql = GameServer.query.filter_by(serverId=db).first()
            dbhost = dbsql.serverHost
            dbname = dbsql.serverName
            session['sqlres'] = runSql(dbhost=dbhost,serverid=db,sql=session.get('cmdsql'))
            print session.get('sqlres')
        return redirect(url_for('main.owsql'))
    return render_template('owsql.html',
                           commandform=commandform,
                           cmdsql=session.get('cmdsql'),
                           serverlists=serverlists,
                           sqlres = session.get('sqlres'),
                           dbname = dbname  <=====问题出现在这里
                           )

注意dbname 的值,现在想把dbname 的值也返回到页面怎么处理?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(1)
黄舟
@main.route('/qqsql', methods=['GET','POST'])
def qqsql():
    cmdsql = None

    commandform = CommandForm()
    serverlists = GameServer.query.all()
    data = {
        "commandform": commandform,
        "serverlists": serverlists
    }
    if commandform.validate_on_submit():
        # global dbname
        session['cmdsql'] = commandform.command.data
        session['serverID'] = request.form.getlist('dbcheckbox')
        for db in session.get('serverID'):
            dbsql = GameServer.query.filter_by(serverId=db).first()
            dbhost = dbsql.serverHost
            dbname = dbsql.serverName
            sqlres = runSql(dbhost=dbhost,serverid=db,sql=session.get('cmdsql'))
            session['sqlres'] = sqlres
            print session.get('sqlres')
            data.update(dbname=dbname, sqlres=sqlres)
        return redirect(url_for('main.owsql'))
    return render_template('owsql.html', **data)

應該只在dbname存在时才向页面传,要不然就然dbname設定預設值

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板