Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a list of values from checkboxes using flask through python

Tags:

python

html

flask

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')
like image 869
Jomonsugi Avatar asked Mar 09 '23 20:03

Jomonsugi


1 Answers

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)
like image 137
Jonathan Avatar answered Apr 28 '23 02:04

Jonathan