我有一个简单的无线字段,它总是导致validate_on_submit返回false。当我打印form.errors时,看起来“not a valid choice”是作为值从radio字段传递的,尽管强制=int
我不认为我正在删除表单中返回的任何内容,我希望以正确的方式创建动态选择。我不明白为什么会失败。
以下是我的项目的相关部分-任何建议,谢谢。
forms.py格式:
class SelectRecord(Form):
rid = RadioField("Record Select", choices=[], coerce=int,validators=[InputRequired()])
视图.py:
@mod.route('/select/', methods=('GET', 'POST'))
@login_required
def select_view():
form = SelectRecord(request.form)
if form.validate_on_submit():
rid = form.data.rid
if form['btn'] == "checkout":
# check out the requested record
Records.checkoutRecord(rid)
return render_template('/records/edit.html',rid=rid)
elif form['btn'] == "checkin":
Records.checkinRecord(rid)
flash("Record checked in.")
else:
mychoices = []
recs_co = session.query(Records.id).filter(Records.editing_uid == current_user.id). \
filter(Records.locked == True)
for x in recs_co:
mychoices.append((x.rid,"%s: %s (%s)" % (x.a, x.b, x.c, x.d)))
x = getNextRecord()
mychoices.append((x.id,"%s: %s (%s %s)" % (x.a, x.b, x.c, x.d)))
form.rid.choices = mychoices
print form.errors
return render_template('records/select.html', form=form)
以及我的模板(select.html):
<form method="POST" action="/select/" class="form form-horizontal" name="select_view">
<h1>Select a record to edit:</h1>
{{ render_field(form.rid, class="form-control") }}
{{ form.hidden_tag() }}
<button type="submit" name="btn" class="btn" value="Check Out">Check Out</button>
<button type="submit" name="btn" class="btn" value="Check In">Check In</button>
</form>
你的领域是这样的。。。
注意,您留下的选项是空列表。你基本上是在说,“没有任何选择对这个领域是有效的”。如果WTForms认为没有任何可供选择的选项,那么您使用的选项将始终无效。
现在,看起来你正试图在下面的else语句中添加这些选项。。。
在运行此操作时,您将能够正确地呈现表单(这发生在方法的末尾)。但是,时间安排得太晚,无法将选择作为验证的一部分提供给表单对象,因为这发生在
validate_on_submit()
中方法顶部附近!尝试使用您用来填写form.rid.choices的代码,并在执行
validate_on_submit
之前运行它。相关问题 更多 >
编程相关推荐