Add a user registration form
flasky/app/auth/forms.py
From FLASK_WTF import Form from wtforms import Stringfield, Passwordfield, Booleanfield, Submitfield from Wtforms.validato RS Import Required, Length, Email, Regexp, Equalto from wtforms import ValidationError from:
Models import User Class LoginForm (Form): email = stringfield (' email ', validators=[required (), Length (1, 64), Email ()]) password = Passwordfield (' Password ', validators=[required ()]) re Member_me = Booleanfield (' Keep me Logged in '), submit = Submitfield (' Log in ') class Registrationform (Form): Email = Stringfield (' email ', validators=[required (), Length (1, +), Email ()]) US Ername = Stringfield (' Username ', validators=[Required (), Length (1, +), Regexp (' ^[a-za-z][a-za-z0-9_.]
*$ ', 0, ' Usernames must has only letters, ' ' numbers, dots or underscores ')] # # #WTForms提供的RegeThe XP validation function ensures that the username field contains only letters, numbers, underscores, and dots.
The two arguments following the regular expression in this validation function are the flags of the regular expression and the error message that is displayed when validation fails. Password = Passwordfield (' password ', validators=[Required (), Equalto (' Password2 ', message= ' passwords must match. ')
)] # # #EqualTo验证函数可以验证两个密码字段中的值是否一致, he's attached to two password fields, and the other field is passed in as a parameter. Password2 = Passwordfield (' Confirm password ', validators=[required ()]) Submit = Submitfield (' Register ') def valid Ate_email (Self, field): If User.query.filter_by (Email=field.data). First (): Raise ValidationError (' Emai
L already registered. ') def validate_username (Self, field): If User.query.filter_by (Username=field.data). First (): Raise Valida
Tionerror (' Username already in use. ') # # # #表单类中定义了以validate_开头且后面跟着字段名的方法, this method is called with a regular validation function.
Add a link to the registration page in the login page template
flasky/app/templates/auth/login.html
{% extends "base.html"%}
{% import "bootstrap/wtf.html" as wtf%}
{% block title%} flasky-login{% Endblock%}
{% block page_content%}
<div class= "Page-header" >
Register page template making
flasky/app/templates/auth/register.html
{% extends "base.html"%}
{% import "bootstrap/wtf.html" as wtf%}
{% block title%} flasky-register{% Endblock%}
{% block page_content%}
<div class= "Page-header" >
User Registration Routing
flasky/app/auth/views.py
From flask import Render_template, redirect, request, Url_for, flash from flask_login import Login_user, Logout_user, Logi N_required, \ Current_User from. Import Auth from: Import db from: Models import User from: Email import send_email from. Forms Import LoginForm, Registrationform @auth. Route ('/login ', methods=[' GET ', ' POST ') de F Login (): Form = LoginForm () if Form.validate_on_submit (): User = User.query.filter_by (email=form.email.d ATA). First () If user is not None and User.verify_password (form.password.data): Login_user (User, FORM.R Emember_me.data) return Redirect (Request.args.get (' next ') or url_for (' Main.index ')) Flash (' Invalid use
Rname or password. ') Return render_template (' auth/login.html ', Form=form) @auth. Route ('/logout ') @login_required def logout (): Logout_use
R () Flash (' You had been logged out. ') Return Redirect (Url_for (' Main.index ')) @auth. Route ('/register ', methods=[' GET ', ' POST ') defRegister (): Form = Registrationform () if Form.validate_on_submit (): User = User (Email=form.email.data, Username=form.username.data, Password=form.password.data) db.session.add (user
) Flash (' You can now login. ') Return Redirect (Url_for (' Auth.login ')) return render_template (' auth/register.html ', form=form)