Skip to content

Instantly share code, notes, and snippets.

@coreylynch
Created May 25, 2012 15:59
Show Gist options
  • Save coreylynch/2788938 to your computer and use it in GitHub Desktop.
Save coreylynch/2788938 to your computer and use it in GitHub Desktop.
WTForms in Flask example pulled from the docs
from wtforms import Form, BooleanField, TextField, PasswordField, validators
class RegistrationForm(Form):
username = TextField('Username', [validators.Length(min=4, max=25)])
email = TextField('Email Address', [validators.Length(min=6, max=35)])
password = PasswordField('New Password', [
validators.Required(),
validators.EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
accept_tos = BooleanField('I accept the TOS', [validators.Required()])
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
user = User(form.username.data, form.email.data,
form.password.data)
db_session.add(user)
flash('Thanks for registering')
return redirect(url_for('login'))
return render_template('register.html', form=form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment