4

I am trying to get a list of values from checkboxes.

I found a post that looks to solve my issue (How to get if checkbox is checked on flask), however, I am continuing to receive this error: Bad Request The browser (or proxy) sent a request that this server could not understand.

I'll post my entire code block to put it in context, but without the checkboxes added, it works fine. In fact, I had the same options as a drop-down menu and it was working fine.

Please note that although I code in python on a regular basis, I am a beginner to html and flask.

               <form action='/user_rec' method='POST' id='submitform'>
                <input type='text', placeholder='user id'  name='user_input'>
                <button type="submit" class="btn btn-success">Recommend</button>
                <br><br>
                <h2>Other Options</h2>
                <h5>Best Number of Players</h5>
                <div class="checkbox">
                  <label><input type="checkbox" name="check" value="1">1</label>
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" name="check" value="2">2</label>
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" name="check" value="3">3</label>
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" name="check" value="4">4</label>
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" name="check" value="5">5+</label>
                </div>
                <h5>Minimum Play Time (minutes)</h5>
                <input type="text", placeholder='0'  name='min_time'>
                <br><br>
                <h5>Maximum Play Time (minutes)</h5>
                <input type="text", placeholder='500000'  name='max_time'>
              </form>

and my python code:

@app.route('/user_rec', methods=['POST'])
def button1():
    user_name = (request.form['user_input'])
    best_num_player = (request.form['best_num_player'])
    min_time = (request.form['min_time'])
    max_time = (request.form['max_time'])
    players = request.form.getlist('check')
1
  • It may be other part of your code. Why not show full your full python code. Commented Jun 17, 2017 at 4:06

1 Answer 1

9

I think there are two problems with your code:

  1. 'best_num_player' is not a key in the form dict.
  2. You didn't return a redirect after posting.

Here's an example app.py that you can try. (I assumed you named the template index.html)

from flask import Flask, render_template, redirect, request

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/user_rec', methods=['POST'])
def user_rec():
    user_name = request.form.get('user_input')
    min_time = request.form.get('min_time')
    max_time = request.form.get('max_time')
    players = request.form.getlist('check')
    print(user_name, min_time, max_time, players)
    return redirect('/')

if __name__ == '__main__':
    app.run(debug=True)
Sign up to request clarification or add additional context in comments.

1 Comment

I should have included my whole decorator. I did include a return statement in my own code, however, you found my mistake. I did not remove the request leading to the best_num_player variable that I had instituted earlier, which was throwing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.