【问题标题】:get a list of values from checkboxes using flask through python通过python使用flask从复选框中获取值列表
【发布时间】:2017-11-19 21:37:53
【问题描述】:

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

我找到了一个帖子,可以解决我的问题 (How to get if checkbox is checked on flask),但是,我继续收到此错误:错误请求浏览器(或代理)发送了一个此服务器无法理解的请求。

我将发布我的整个代码块以将其置于上下文中,但没有添加复选框,它可以正常工作。事实上,我有与下拉菜单相同的选项,而且效果很好。

请注意,虽然我经常使用 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>

还有我的python代码:

@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')

【问题讨论】:

  • 它可能是您代码的其他部分。为什么不显示完整的 Python 代码。

标签: python html flask


【解决方案1】:

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

  1. 'best_num_player' 不是 dict 形式的键。
  2. 您在发布后没有返回重定向。

这是一个示例app.py,您可以尝试。 (我假设您将模板命名为 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)

【讨论】:

  • 我应该包括我的整个装饰器。我确实在自己的代码中包含了一个 return 语句,但是,你发现了我的错误。我没有删除导致我之前设置的 best_num_player 变量的请求,这引发了错误。
猜你喜欢
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多