首页 > flask 同一页面两个form怎么处理。

flask 同一页面两个form怎么处理。

页面:

html内容:

<div class="container" style="margin-top: 40px">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">

            {% for messages in get_flashed_messages() %}
                <div class="alert alert-warning">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                       {{ messages }}
                </div>
            {% endfor %}

            <h2 class="col-md-offset-2">WW打补丁操作</h2><br>
            <form  method="POST">
                <label for="name">选服进行打补丁</label>
                    <div class="form-group">
                        <div id="list">
                            {% for server in serverlists %}
                            <label class="checkbox-inline">
                                <input type="checkbox" name="dbcheckbox"
                                       id="{{ server.serverId }}"
                                       value="{{ server.serverId }}">
                                {{ server.serverName }}
                            </label>
                            {% if  loop.index%10 ==0 %}
                                <br>
                            {% endif %}
                            {% endfor %}
                        </div>
                    <br>
                    <input type="checkbox" id="all">
                    <input type="button" value="全选" class="btn" id="selectAll">
                    <input type="button" value="全不选" class="btn" id="unSelect">
                    <input type="button" value="反选" class="btn" id="reverse">
                    <input type="button" value="获得选中的所有值" class="btn" id="getValue">
                    </div>

                <div class="form-group" style="margin-top: 40px">
                    {{ patchform.hidden_tag() }}
                    {{ render_field(patchform.patchcommand,class="form-control",placeholder="Patch Command") }}
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>

            <br>
            <div>
                <form action="upload" method="POST" enctype="multipart/form-data">
                    <input type="file" name="file"><br />
                    <input type="submit" value="Upload">
                </form>
            </div>

        </div>

    </div>
</div>

服务端内容:

@ww.route('/upload',methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        if file and allww_file(file.filename):
            upload_path = app.config['UPLOAD_FOLDER']
            if not os.path.exists(upload_path):
                os.mkdir(upload_path)
            upload_file = path.join(upload_path,secure_filename(file.filename))
            file.save(upload_file)
            flash('upload file %s complete!' %file.filename)
            return redirect(url_for('ww.upload'))
        else:
            flash('nothin file select or file type error!')
    return render_template('ww/patchww.html')


@ww.route('/Patchww',methods=['GET','POST'])
def Patchww():
    cmdpatch = None
    serverlists = wwGame.query.all()
    patchform = PatchCommandForm()
    if patchform.validate_on_submit():
        session['cmdpatch'] = patchform.patchcommand.data
        session['serverID'] = request.form.getlist('dbcheckbox')


        #判断是否选服
        if session.get('serverID') == []:
            flash(u'未选游戏服,请勾选游戏服!')
            return redirect(url_for('ww.Patchww'))

        for db in session.get('serverID'):
            dbsql = wwGame.query.filter_by(serverId=db).first()
            dbhost = dbsql.serverHost
            dbname = dbsql.serverName
            session['dbname'] = dbname
            session['dbhost'] =dbhost
            print session.get('dbhost')
        print session.get('cmdpatch')
        return redirect(url_for('ww.Patchww'))
    return render_template('ww/patchww.html',
                           serverlists=serverlists,
                           patchform=patchform,
                           )

当使用第二个form时,也就是上传文件的时候:

出现了:'patchform' is undefined
但是 patchform 是第一个form的定义内容。这为咋提交的时候会触发第一个from 的?

===================

使用ajax解决了,新问题出现了

form1,form2其实对应了两个后端的views,当form2的views上传完文件后,form1的views里要怎样获得刚刚上传的文件的文件名?
即:
@ww.route('/Patchww',methods=['GET','POST']) 要怎样取到@ww.route('/upload',methods=['POST','GET'])这里面的file.filename值?


Ajax提交到不同链接不就好了

【热门文章】
【热门文章】