Creating views

The login and registration views will create our form objects and pass them to the templates. After the LoginForm validates the user's credentials, we will use Flask-Login to actually log the user in.

In the auth/controllers.py controller, we will find the login view, as shown in the following code:

...from flask_login import login_user, logout_user...@auth_blueprint.route('/login', methods=['GET', 'POST'])@oid.loginhandlerdef login():    form = LoginForm()    ...    if form.validate_on_submit():        user = User.query.filter_by(username=form.username.data).one()        login_user(user, remember=form.remember.data)        ...        flash("You have been logged in.", category="success")        return redirect(url_for('main.index'))    ...    return render_template('login.html' ...

Get Mastering Flask Web Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.