一、问题描述
表单定义如下:
class UserInfoEditForm(FlaskForm):"""用户信息编辑表单"""new_username = StringField("用户名", validators=[DataRequired()])new_email = StringField("邮箱", validators=[DataRequired(), Email()])new_password = PasswordField("密码", validators=[DataRequired()])new_password2 = PasswordField("确认密码", validators=[DataRequired(), EqualTo("new_password", message="密码与确认密码不一致!")])submit = SubmitField("提交")def __init__(self, origin_username, origin_email, *args, **kwargs):super(UserInfoEditForm, self).__init__(origin_username, *args, **kwargs)self.origin_username = origin_usernameself.origin_email = origin_emaildef validate_username(self, new_username):if self.origin_username != new_username.data:user = User.query.filter_by(username=self.new_username.data).first()if user is not None:raise ValidationError("该用户名已被使用!")def validate_email(self, new_email):if self.origin_email != new_email.data:user = User.query.filter_by(email=self.new_email.data).first()if user is not None:raise ValidationError("该邮箱账号已被使用!")
提交表单时,发现提交的参数均为空:
二、问题定位
1、经过排查发现在继承父类FlaskForm时,多传了一个参数:
super(UserInfoEditForm, self).init(origin_username, *args, **kwargs)
2、查看父类FlaskForm初始化源码:
3、果断发现super(UserInfoEditForm, self).init(origin_username, *args, **kwargs)这种继承父类FlaskForm的方式不对,改为以下方式就OK了:
super(UserInfoEditForm, self).__init__(*args, **kwargs)