我有一个烧瓶形式,这是代码的一部分:
class BaseForm(FlaskForm):
username = TextField('Username')
email = TextField('Email', [validators.Required('Please enter your email address.'), validators.Length(min=6, message=('Too short for an email address?')), validators.Email(message=('Enter a valid email address.'))
])
password = PasswordField('Password')
register = SubmitField('Register')
class RegisterForm(BaseForm):
BaseForm.username
BaseForm.email
BaseForm.password
BaseForm.register
当我使用上面的 代码时,不会显示错误消息 ,但是,当我使用下面的 代码时,则会显示错误< / em> :
class BaseForm(FlaskForm):
username = TextField('Username')
email = TextField('Email', [validators.Required('Please enter your email address.'), validators.Length(min=6, message=('Too short for an email address?')), validators.Email(message=('Enter a valid email address.'))
])
password = PasswordField('Password')
register = SubmitField('Register')
class RegisterForm(BaseForm):
BaseForm.username
email = TextField('Email', [validators.Required('Please enter your email address.'), validators.Length(min=6, message=('Too short for an email address?')), validators.Email(message=('Enter a valid email address.'))
])
BaseForm.password
BaseForm.register
所以在我看来,这里的问题是 验证未随字段一起继承 ,或者我听不懂。我试图减少正在编写的代码。 请告诉我如何使用我的第一个代码,或者如果不可能完成任务,请帮助我实现我尝试的表单继承
答案 0 :(得分:0)
您的RegisterForm没有来自BaseForm的新字段。 无论如何,不要在子类中重新定义您的字段。 也许您应该首先阅读一些编程语言,例如Python,并特别注意继承。
只需执行以下操作:
class BaseForm(FlaskForm):
username = TextField('Username')
email = TextField('Email', [validators.Required('Please enter your email address.'), validators.Length(min=6, message=('Too short for an email address?')), validators.Email(message=('Enter a valid email address.'))])
password = PasswordField('Password')
class RegisterForm(BaseForm):
register = SubmitField('Register')
答案 1 :(得分:0)
class BaseForm(Form):
def __iter__(self):
token = self.csrf_token
yield token
field_names = {token.name}
for cls in self.__class__.__bases__:
for field in cls():
field_name = field.name
if field_name not in field_names:
field_names.add(field_name)
yield self[field_name]
for field_name in self._fields:
if field_name not in field_names:
yield self[field_name]
class EmailForm(BaseForm):
email = EmailField('Email')
class PasswordForm(BaseForm):
password = PasswordField('Password')
class EmailPasswordForm(EmailForm, PasswordForm):
pass