AttributeError: Object Has No Attribute 'user_loader'
Solution 1:
First, your User.get_id
should be returning unicode
not an int
. The documentation mentions this, along with an example:
This method must return a unicode that uniquely identifies this user, and can be used to load the user from the user_loader callback. Note that this must be a unicode - if the ID is natively an int or some other type, you will need to convert it to unicode. (Your User Class)
So that needs to be changed to:
def get_id(self):
return unicode(self.id)
Next up, your user_loader
. From the docs:
This sets the callback for reloading a user from the session. The function you set should take a user ID (a unicode) and return a user object, or None if the user does not exist.
Which would mean adjusting your user_loader
to be something like:
@login_manager.user_loader
def get_user(user_id):
try:
return User.query.get(int(user_id))
except:
return None
Also, you have an error here, which is likely the direct cause of the error:
login_manager = login_message_category = 'info'
So your taking your login_manager
and replacing it with a string with the contents 'info'
. So later when your app tries to access login_manager.user_loader
it's failing, because a string 'info'
doesn't have a user_loader
method.
Changing it to the below should fix the error. Though the other issues addressed above also need to be implemented.
login_manager.login_message_category = 'info'
Solution 2:
You have used the login_manager = LoginManager(app)
you are creating an object along with the configuration. Insider of that create an object first and configure the object in 2 steps.
login_manager = LoginManager()
login_manager.init_app(app)
for more reference please check the link here[https://flask-login.readthedocs.io/en/latest/]
you may need to update in your init.py file.
Post a Comment for "AttributeError: Object Has No Attribute 'user_loader'"