app route login methods GET POST def login form LoginForm if form vali

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(user_email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
login_user(user, form.remember_me.data)
return redirect(request.args.get('next') or url_for('index'))
flash('Invalid username or password.')
return render_template('login.html', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
user = User(user_name=form.user_name.data,
user_email=form.user_email.data,
password=form.password.data)
db.session.add(user)
flash('You can now login')
return redirect(request.args.get('next') or url_for('login'))
return render_template('register.html', form=form)