Created
May 25, 2012 15:59
-
-
Save coreylynch/2788938 to your computer and use it in GitHub Desktop.
WTForms in Flask example pulled from the docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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