通过python使用flask从复选框中获取值列表

通过python使用flask从复选框中获取值列表,python,html,flask,Python,Html,Flask,我正在尝试从复选框中获取值列表 我发现一篇文章似乎解决了我的问题(),但是,我继续收到这个错误:请求错误浏览器(或代理)发送了一个此服务器无法理解的请求 我将发布我的整个代码块以将其放到上下文中,但是如果不添加复选框,它就可以正常工作。事实上,我有与下拉菜单相同的选项,它工作正常 请注意,尽管我经常使用python编写代码,但我是html和flask的初学者 <form action='/user_rec' method='POST' id='submitfo

我正在尝试从复选框中获取值列表

我发现一篇文章似乎解决了我的问题(),但是,我继续收到这个错误:请求错误浏览器(或代理)发送了一个此服务器无法理解的请求

我将发布我的整个代码块以将其放到上下文中,但是如果不添加复选框,它就可以正常工作。事实上,我有与下拉菜单相同的选项,它工作正常

请注意,尽管我经常使用python编写代码,但我是html和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>

我认为您的代码有两个问题:

  • “最佳球员”
    不是dict格式的键
  • 发布后您没有返回重定向
  • 下面是一个可以尝试的示例
    app.py
    。(我假设您将模板命名为
    index.html


    它可能是代码的另一部分。为什么不完整地显示您的完整python代码呢?我应该包括我的整个装饰程序。我确实在自己的代码中包含了一个return语句,但是,您发现了我的错误。我没有删除导致我之前设置的best_num_player变量的请求,该变量抛出了错误。
    @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')
    
    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)