cari

Rumah  >  Soal Jawab  >  teks badan

python - flask返回值到页面问题

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中文网2808 hari yang lalu287

membalas semua(1)saya akan balas

  • 黄舟

    黄舟2017-04-17 17:31:37

    @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)

    hanya perlu dihantar ke halaman apabila dbname wujud, jika tidak dbnametetapkan nilai lalai

    balas
    0
  • Batalbalas