描述
使用request.args.get方法获取web前段数据填充flask-wtf表单时,由于获取的值为unicode编码,导致flask-wtf验证失败。
问题排查
flask-wtf 后台的Form表单, selectField字段关联数据库,代码如下:
class ApplyReourceForm(FlaskForm):
username = StringField('User Name')
apply_pool_id = SelectField('Apply Pool Name', coerce=int)
apply_date = StringField('Apply Date')
audit_user_id = SelectField('Audit User Name', coerce=int)
apply_reason = TextAreaField('Apply Reason')
submit = SubmitField('Submit')
save = SubmitField('Save')
''' SelectField字段关联数据库,在此初始化选项value值(其中User,ResourcePool 为数据库模型) '''
def __init__(self, *args, **kwargs):
super(ApplyReourceForm, self).__init__(*args, **kwargs)
self.apply_pool_id.choices = [(pool.id, pool.name) for pool in
ResourcePool.query.all()]
self.audit_user_id.choices = [(audit_user.id, audit_user.username) for audit_user in
User.query.all()]
调用view代码
@yarn.route('/apply', methods=['GET', 'POST'])
@login_required
def apply_resource_pool():
resource_id = request.args.get('resource_id')
form = ApplyReourceForm()
form.username.data = current_user.username # form表单值预填充
form.apply_pool_id.data = resource_id # form表单值预填充,值来自web前台
form.apply_date.data = time.strftime('%Y-%m-%d %H:%M:%S')
if form.validate_on_submit():
if form.save.data:
print 'click save button'
elif form.submit.data:
print 'click submit button'
return render_template('yarn/apply_resource_pool.html', form=form)
前台form表单验证总是不通过,查看validate_on_submit 类,分别打印is_submitted() 及 validate()值
def validate_on_submit(self):
"""Call :meth:`validate` only if the form is submitted.
This is a shortcut for ``form.is_submitted() and form.validate()``.
"""
print 'submit: %s' % self.is_submitted()
print 'validate: %s' % self.validate()
return self.is_submitted() and self.validate()
执行结果:
submit: True
validate: False
触发了submit方法,但是表单验证未通过,继续查看validate方法:
def validate(self):
"""
Validates the form by calling `validate` on each field, passing any
extra `Form.validate_<fieldname>` validators to the field validator.
"""
extra = {}
for name in self._fields:
inline = getattr(self.__class__, 'validate_%s' % name, None)
if inline is not None:
extra[name] = [inline]
return super(Form, self).validate(extra) # 返回在这一句
继续查看super.validate()方法,在循环校验字段处依次打印每个字段的校验结果:
def validate(self, extra_validators=None):
"""
Validates the form by calling `validate` on each field.
:param extra_validators:
If provided, is a dict mapping field names to a sequence of
callables which will be passed as extra validators to the field's
`validate` method.
Returns `True` if no errors occur.
"""
self._errors = None
success = True
for name, field in iteritems(self._fields):
if extra_validators is not None and name in extra_validators:
extra = extra_validators[name]
else:
extra = tuple()
print extra # 传入的校验方法
if not field.validate(self, extra):
print '------------------------ %s -------error--------------------------' % name
success = False
return success
执行结果:
()
()
------------------------ apply_pool_id -------error--------------------------
()
()
()
()
()
()
目前可以看出是apply_pool_id校验失败导致表单无法通过验证,在view处查看提交时表单的字段值及类型,代码如下:
print 'apply_pool_id: %s, %s' % (form.apply_pool_id.data, type(form.apply_pool_id.data))
print 'audit_user_id: %s, %s' % (form.audit_user_id.data, type(form.audit_user_id.data))
执行结果:
apply_pool_id: 1, <type 'unicode'>
audit_user_id: 1, <type 'int'>
发现apply_pool_id值类型为unicode与定义的int不符,导致表单验证失败,于是修改view视图,将参数强制转换为int类型:
resource_id = int(request.args.get('resource_id'))
再次提交表单,日志信息:
submit: True
()
()
()
()
()
()
()
()
validate: True
()
()
()
()
()
()
()
()
click submit button
apply_pool_id: 1, <type 'int'>
audit_user_id: 1, <type 'int'>
问题解决
发现验证已通过,以前没遇到过这种情况,网上也没找到对应的问题,所以在此记录一下。 PS:遇到问题还是要多去熟悉源码。