How to set variables with a inputbox in Flask


I want to declare variables in Flask using input boxes.

However, I get this error message:

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser
(or proxy) sent a request that this server could not understand.
KeyError: ‘column1’

But i cleary named my inputbox…

My Flask code :

@app.route('/', methods=['POST', 'GET'])
def get_columns():
    col1 = request.form["column1"]
    col2 = request.form["column2"]
    return col1, col2

def upload_file():
    file = request.files['file']
    col1,col2 =get_columns()


    if file.filename == '':
        flash('No file selected for uploading','red')
        # return redirect(request.url)
        return render_template('index.html', disp_div = disp_div)
    if file and allowed_file(file.filename):
        return render_template('index.html', output=output)
        # return redirect('/')
        flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif', 'red')
        # return redirect(request.url)
        return render_template('index.html')

My jinja template index.html:

              <td><form action="/" method="POST">
                      <input name="column1" id="column1" placeholder="Spaltenname der XXX">

              <td><form action="/" method="POST">
                        <input name="column2" id="column2" placeholder="Spaltenname der YYY">


You have 2 forms in your HTML.
Both of them point to get_columns().
But the python code assume it will get both of them (column1 AND column2).
This is wrong – the python code will get column1 OR column2 – not both.

Answered By – balderman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published