简体   繁体   中英

How to include more than one HTML-form in a FLASK view?

I´m building a little web application using python 2.7 and flask (my first one).

I´m trying to include three HTML forms in one flask view to have all three of the forms on one webpage:

  1. One form to allow data upload (will be an XML-file)
  2. One Button to start a python process, that performs analysis on the XML-file
  3. One Button that allows download of the results of step 2

While I got it working, when using a separate view for each of the three steps, I somehow can´t get it done when trying to include all three steps in one view. Does anyone know what I am doing wrong?

Here´s my code so far. The HTML-code is included in the python script. Working with templates didn´t help me either. The HTML page is working, but the buttons don´t perform any action.

...
@app.route('/PAM', methods=['GET', 'POST'])
def start_PAM():
    if request.method == 'POST':
        if "Upload" in request.form.values():
                file = request.files['file']
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    print "File upload complete !!"
                    return redirect('/PAM')
        elif "Start PAM" in request.form.values():
                import os
                os.system("D:\Python_Test\PAM\PAM_1st_Try.py")
                print "PAM Process Finished!!"
                return redirect('/PAM')
        elif "Download" in request.form.values():
                filename = "FILE.XML"
                return redirect(url_for('download_file', filename=filename))

    return '''
    <!doctype html>
    <title>Start PAM Process</title>
    <h1>XML Upload</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    <p><h1>Start the PAM Process</h1>
    <form name='Start PAM' method='POST'>
    <p><input type="submit" value="PAM" class="submitButton"/>
    </form>

    <p><h1>Download Result</h1>
    <form name='Download' method='POST'>
    <p><input type="submit" value="Download" class="submitButton"/>
    </form>
    '''

@app.route("/downloads/<filename>")
def download_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

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

I think the step you're missing is to assign an action to the forms. At the moment the HTML doesn't know what to do with the form?

http://www.w3schools.com/tags/att_form_action.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM