【问题标题】:Receive 'None' from select field as value in post request从选择字段接收“无”作为发布请求中的值
【发布时间】:2020-02-14 05:41:32
【问题描述】:

我也是新来的 Flask。我收到“无”而不是来自选择字段的值作为响应。所以会很感激你的帮助!

这是我的布局:

<form method="post">
    <!-- select -->
    <div class="form-group mt-4">  
      <legend for="Sel">Choose criteria to sort</legend>
      <select class="custom-select" id="Sel" name="sel"> <!-- multiple -->       
        {% for field in form.select %}
          <option value="{{ field }}"></option>    
        {% endfor %}      
      </select>
    </div>
    <!-- submit -->
    {{form.submit(class="btn btn-primary")}}
  </form>

这是我的观点:

from flask import Flask, render_template, request
from wtforms import Form, SelectField, SubmitField


app = Flask(__name__)


sel_choice = [ 
    ('sex', 'gender of the person interviewed '), 
    ('city', 'city where pool took place'),
    ('emotion', 'emotional characteristic of person`s comment'),
    ('month', 'month of poll'),
    ('poll_time', 'poll time')
]

class ChoiceForm(Form):
    select = SelectField(u'Criteria', choices=sel_choice)
    submit = SubmitField(label='apply')

@app.route('/', methods=['POST', 'GET'])
def sel():
    form =  ChoiceForm(request.form)
    if request.method == 'POST':
        print(form.select.data)
    return render_template('forms.html', form=form)

这是我从服务器收到的:

  • 服务 Flask 应用“test.py”(延迟加载)
  • 环境:发展
  • 调试模式:开启
  • http://127.0.0.1:5000/ 上运行(按 CTRL+C 退出)
  • 使用 stat 重启
  • 调试器已激活!
  • 调试器 PIN:231-471-963

    127.0.0.1 - - [17/Oct/2019 13:00:55] "POST / HTTP/1.1" 200 -

【问题讨论】:

  • 检查您是否从 'print(request.form.get('sel'))' 获得价值。
  • 它的工作;)谢谢!

标签: forms flask


【解决方案1】:

改变

print(form.select.data)

print(request.form.get('sel'))'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-15
    • 2015-09-18
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多