Luis Miguel
Luis Miguel

Reputation: 212

Bad request with two submit buttons

I'm creating a web page with Flask and I'm new using it. I want that the user paste some text in a textarea and I make a prediction with that text, but also the user can upload a file and make a prediction with the text in that file.

Here is my HTML file:

<!DOCTYPE html>
<html>
    <body>
        <p>Please type data or upload a file:</p>
        <p>
            {% with messages = get_flashed_messages() %}
                {% if messages %}
                <ul class=flashes>
                {% for message in messages %}
                    <li>{{ message }}</li>
                {% endfor %}
                </ul>
                {% endif %}
            {% endwith %}
        </p>
        <form method="POST" action="{{ url_for('display_result') }}" enctype="multipart/form-data">
            <textarea id="data" name="data" cols="100" rows="50"></textarea>
            <br/>
            <input type="submit" name="prediction" value="Predict text">
            <input type="file" id="file_u" name="file_u" value="Upload a file to predict" requiered>
            <input type="submit" name="prediction_file" value="Predict file">
        </form>
    </body>
</html>

Here is the function of Python:

def display_result():
    if request.method == "POST":
        if request.form.get("prediction") == "Predict text":
            if request.form["data"] == "":
                flash("Please paste some data")
                return redirect(url_for("home"))
            else:    
                #Make a prediction with the data in the textarea
                return render_template("result.html", result=result)




        elif request.form.get("prediction_file") == "Predict file":

            if request.files['file_u'].filename == '':
                flash("No file selected for uploading")
                return redirect(url_for("home"))
            else:
                upload_file = request.files["file_u"]
                data = upload_file.read()

                #Make a prediction with the file uploded

                return render_template("result.html", result=result)

The problem is that when a user does not select a file I want that page shows an error but give a werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'file_u'.

Upvotes: 0

Views: 85

Answers (1)

AdamGold
AdamGold

Reputation: 5071

You can check for the existence of the file before trying to get its value:

        elif request.form.get("prediction_file") == "Predict file":
            file_object = request.files.get("file_u")
            if not file_object or not file_object.filename:
                flash("No file selected for uploading")
                return redirect(url_for("home"))
            else:
                data = file_object.read()

                #Make a prediction with the file uploded

                return render_template("result.html", result=result)

Upvotes: 1

Related Questions