forms - 从选择字段接收“无”作为发布请求中的值
问题描述
我也是新来的 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 - - [2019 年 10 月 17 日 13:00:55] “POST / HTTP/1.1” 200 -
解决方案
改变
print(form.select.data)
至
print(request.form.get('sel'))'
推荐阅读
- qr-code - 寻找有关自定义可读代码的文档
- java - 只接受 8 位数字
- azure - 使用用户凭据从窗口桌面应用程序调用 azure 资源管理器存储 API
- python - 使用 selenium Select 导航下拉菜单
- java - 在 TestNG 中执行时 XML 中出现启动错误 - 无法继续执行测试 - Selenium
- python - 如何编写在其内部连接中使用复杂“on”子句的 Django 查询?
- php - PHP curl 在 5.6 和 5.4 (Centos7) 中损坏
- python - 如何在没有递归的情况下添加条目,以及从任何嵌套字典中的特定键中检索、更改或删除值?
- json - 尝试运行 npm run dev “未找到条目。”
- android - 向用户显示 ArrayList