How can i fix the error: AttributeError: 'Flask' object has no attribute 'login_manager'

I'm trying to login into a simple note taking website, but when I press the button Login this error shows up: AttributeError: 'Flask' object has no attribute 'login_manager'

This is the relevant code:

from flask import Blueprint, render_template, request,flash, redirect, url_for
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, login_required, logout_user, current_user

from .models import db,DB_NAME,User, Note

auth = Blueprint('auth', __name__)
@auth.route('/login',methods = ['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        if user:
            if check_password_hash(user.password, password):
                flash('Logged in successfully!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('views.home'))
            else:
                flash('Incorrect password, try again.', category='error')
        else:
            flash('Email does not exist.', category='error')

    return render_template("login.html", user=current_user)

The issue is raised at:

login_user(user, remember=True)

I've no idea on how to fix this. I would really appreciate some help.

1 answer

  • answered 2021-05-16 06:33 J.G.

    You have not configured Flask-Login. You need to add the following lines.

    login_manager = LoginManager()
    login_manager.init_app(app)
    

    This is described in the documentation, see https://flask-login.readthedocs.io/en/latest/