我正在努力修改cookiecutter Flask应用。
我的表格如下:
<form class="form-inline" id="registerForm" method="POST" action="/register/" role="form">
<div class="form-group">
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address">
</div>
<button type="submit" class="btn btn-warning btn-lg">Submit!</button>
</form>
和'用户'模型看起来像:
class User(UserMixin, SurrogatePK, Model):
__tablename__ = 'users'
username = Column(db.String(80), unique=True, nullable=True)
email = Column(db.String(80), unique=True, nullable=False)
#: The hashed password
password = Column(db.String(128), nullable=True)
created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
first_name = Column(db.String(30), nullable=True)
last_name = Column(db.String(30), nullable=True)
active = Column(db.Boolean(), default=False)
is_admin = Column(db.Boolean(), default=False)
confirmed = db.Column(db.Boolean, nullable=False, default=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
def __init__(self, username, email, password=None, **kwargs):
db.Model.__init__(self, username, email=email, **kwargs)
if password:
self.set_password(password)
else:
self.password = None
我的计划是只询问一封电子邮件,验证它,然后让用户在验证并登录后再填写其余字段。所以我想创建整个对象,但只有电子邮件首先填补了字段。当我通过仅提交电子邮件测试电子邮件注册表单(上面的html)时,我获得了屏幕截图。我知道cookie切割器使用使用flask-wtf构建的表单,但我不确定为什么我要激活此表单验证。为什么我的方法有误?
答案 0 :(得分:1)
从源代码中您可以知道:user.py
class RegisterForm(Form):
username = TextField('Username', validators=[DataRequired(), Length(min=3, max=25)])
first_name = TextField('first_name', validators=[DataRequired(), Length(min=3, max=25)])
last_name = TextField('last_name', validators=[DataRequired(), Length(min=3, max=25)])
email = TextField('Email', validators=[DataRequired(), Email(), Length(min=6, max=40)])
password = PasswordField('Password', validators=[DataRequired(), Length(min=6, max=40)])
confirm = PasswordField('Verify password', [DataRequired(), EqualTo('password', message='Passwords must match')])
您可以看到有六个字段,所有字段都是必需(通过DataRequired
验证程序),这就是您看到六条错误消息的原因
请注意,cookiecutter-flask
仅为您提供基本结构,您需要自己修改一些文件。