AttributeError: 'Flask' object has no attribute 'login_manager' — Login_Manager
Question:
I am having issue with Login_manager in my application . The error is thrown here
def authenticate(form):
if form.validate_on_submit():
try:
user = session.query(User).filter(User.email == form.email.data).first()
except :# models.DoesNotExist:
flash("Your email or password does not match !", "error")
else :
if check_password_hash(user.password,form.password.data):
login_user(user)
flash("You've been logged in", "success")
return redirect(url_for('index'))
else :
flash("Your email or password does not match !", "error")
return render_template('login.html',form = form)
The issue is raise at
login_user(user)
my login_manager is configured below the autheticate as follows.
if __name__ == '__main__':
app.secret_key = 'Innalhamdulillah.nahmaduhu.taalanastainubihi.wanastagfiruh!'
app.run(debug = DEBUG, host=HOST, port= PORT)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
try:
return session.query(User).filter(User.id == userid).first()
except models.DoesNotExist :
return None
and in my declaration
from flask.ext.login import LoginManager,login_user
Please how do i addressed this issue and what is the cause ? How do i better understand it ? I was suspecting my def load_user(userid):
bcos i am not very much clear and this my first. Any help would be appreciated.
Answers:
You run your app before initializing the LoginManager.
So you should have:
app.secret_key = 'xxxxyyyyyzzzzz'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
app.run(debug = DEBUG, host=HOST, port= PORT)
I’m just learning Flask, and I encountered this problem too. I found that I had not initialized the login_manager to the app.
So I changed:
login_manager = LoginManager()
to
login_manager = LoginManager(app)
and that solved my issue.
All flask extensions need to be initilized. Initialize the login manager for use in the app like this:
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
and don’t forget this:
login_manager.init_app(app)
If it is not initialized it will raise errors that look like this:
Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.
or something like this:
AttributeError: 'Flask' object has no attribute 'login_manager'
or others.
the linelogin_manager.init_app(app)
is the key here.
I know that this has been answered some time ago already, but I came across this thread when I had the same problem, and none of the solutions worked for me.
In my case, my program that I had built yonks ago, started throwing this exception: Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.
After a bit of troubleshooting, I had found that it was python module: Flask-Login
, I had reloaded my PC and when installing all my software again, the version of Flask-Login
was now 0.5.0
.
I removed that version of Flask-Login
and replaced it with the version I had before my reload, with cmd:
pip install -Iv Flask-Login==0.4.1
And waaalaaa…. it was fixed.
Hope this helps someone like it did me.
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(id=user_id).first()
I am having issue with Login_manager in my application . The error is thrown here
def authenticate(form):
if form.validate_on_submit():
try:
user = session.query(User).filter(User.email == form.email.data).first()
except :# models.DoesNotExist:
flash("Your email or password does not match !", "error")
else :
if check_password_hash(user.password,form.password.data):
login_user(user)
flash("You've been logged in", "success")
return redirect(url_for('index'))
else :
flash("Your email or password does not match !", "error")
return render_template('login.html',form = form)
The issue is raise at
login_user(user)
my login_manager is configured below the autheticate as follows.
if __name__ == '__main__':
app.secret_key = 'Innalhamdulillah.nahmaduhu.taalanastainubihi.wanastagfiruh!'
app.run(debug = DEBUG, host=HOST, port= PORT)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
try:
return session.query(User).filter(User.id == userid).first()
except models.DoesNotExist :
return None
and in my declaration
from flask.ext.login import LoginManager,login_user
Please how do i addressed this issue and what is the cause ? How do i better understand it ? I was suspecting my def load_user(userid):
bcos i am not very much clear and this my first. Any help would be appreciated.
You run your app before initializing the LoginManager.
So you should have:
app.secret_key = 'xxxxyyyyyzzzzz'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
app.run(debug = DEBUG, host=HOST, port= PORT)
I’m just learning Flask, and I encountered this problem too. I found that I had not initialized the login_manager to the app.
So I changed:
login_manager = LoginManager()
to
login_manager = LoginManager(app)
and that solved my issue.
All flask extensions need to be initilized. Initialize the login manager for use in the app like this:
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
and don’t forget this:
login_manager.init_app(app)
If it is not initialized it will raise errors that look like this:
Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.
or something like this:
AttributeError: 'Flask' object has no attribute 'login_manager'
or others.
the linelogin_manager.init_app(app)
is the key here.
I know that this has been answered some time ago already, but I came across this thread when I had the same problem, and none of the solutions worked for me.
In my case, my program that I had built yonks ago, started throwing this exception: Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.
After a bit of troubleshooting, I had found that it was python module: Flask-Login
, I had reloaded my PC and when installing all my software again, the version of Flask-Login
was now 0.5.0
.
I removed that version of Flask-Login
and replaced it with the version I had before my reload, with cmd:
pip install -Iv Flask-Login==0.4.1
And waaalaaa…. it was fixed.
Hope this helps someone like it did me.
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(id=user_id).first()