forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=15)])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password',validators=[DataRequired(), Length(min=8)])
confirm_password = PasswordField('Confirm Password',validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Sign Up')
flaskblog.py
def register():
form = RegistrationForm()
if form.validate_on_submit():
flash(f'Account created for {form.username.data}!', 'success')
return redirect(url_for('home'))
return render_template('register.html', title='Register', form=form)
register.html
<div class="form-group">
{{ form.username.label(class='form-control-label')}}
{% if form.username.errors %}
{{ form.username(class='form-control form-control-lg is-invalid')}}
<div class="invalid-feedback">
{% for error in form.username.errors %}
<span>{{error}}</span>
{% endfor %}
</div>
{% else %}
{{ form.username(class='form-control form-control-lg')}}
{% endif %}
在register.html中,其他字段也是如此