AttributeError:对象没有属性'user_loader' [英] AttributeError: Object has no attribute 'user_loader'
问题描述
我正在使用Flask框架对具有身份验证的网站进行编程.我已经尝试了在互联网上找到的所有解决方案,但没有任何解决方案.
I'm programming a Website with Authentification while using the Flask Framework. I've tried every solution that i found on the internet but nothing worked for me.
我的第一个想法是,项目结构已损坏.例如缺少其他文件的导入.但这不是我想的问题.
My first idea was, that the Project Structure was corrupt. e.g. missing imports from other files. But thats not the problem i think.
我的models.py文件:
My models.py File:
from flask_login import UserMixin, LoginManager
from flaskapp import db, login_manager
@login_manager.user_loader
def get_user(user):
try:
return get_id(user)
except:
return None
class User(db.Model,UserMixin):
id =db.Column(db.Integer, primary_key=True)
username =db.Column(db.String(20),unique=True, nullable=False)
email =db.Column(db.String(120), unique=True, nullable=False)
password =db.Column(db.String(60), nullable=False)
powerlevel =db.Column(db.Integer, nullable=False)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return int(self.id)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.powerlevel}')"
我的 init .py文件:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
app = Flask(__name__)
app.config['SECRET_KEY'] = 'xxx'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager = login_message_category = 'info'
from flaskapp import routes
使用以下命令运行WebApp时:
When running the WebApp using:
export FLASK_APP = run.py DEBUG = TRUE烧瓶运行
export FLASK_APP=run.py DEBUG=TRUE flask run
出现以下错误消息:
Traceback (most recent call last):
File "/home/osboxes/.local/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 966, in main
cli.main(prog_name="python -m flask" if as_module else None)
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/home/osboxes/.local/lib/python3.6/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/osboxes/.local/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/osboxes/.local/lib/python3.6/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/osboxes/.local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/osboxes/.local/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/home/osboxes/.local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 305, in __init__
self._load_unlocked()
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/home/osboxes/.local/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/osboxes/Desktop/HMI/run.py", line 1, in <module>
from flaskapp import app
File "/home/osboxes/Desktop/HMI/flaskapp/__init__.py", line 21, in <module>
from flaskapp import routes
File "/home/osboxes/Desktop/HMI/flaskapp/routes.py", line 6, in <module>
from flaskapp.models import User
File "/home/osboxes/Desktop/HMI/flaskapp/models.py", line 7, in <module>
@login_manager.user_loader
AttributeError: 'str' object has no attribute 'user_loader'
现在我不知道还有什么问题.如果我忘记提供解决错误的代码,请告诉我.谢谢您的帮助!
Right now i don't know what else could be the problem. If i forgot to supply some code for solving the error, let me know. Thank you for your Help!
推荐答案
首先,您的 User.get_id
应该返回 unicode
而不是 int
.文档中提到了这一点,并附带了一个示例:
First, your User.get_id
should be returning unicode
not an int
. The documentation mentions this, along with an example:
此方法必须返回唯一标识此用户的unicode,并且可以用来从user_loader回调中加载用户.笔记必须是unicode-如果ID本身是int或某些其他类型,则需要将其转换为unicode.(您的用户类)
因此需要更改为:
def get_id(self):
return unicode(self.id)
接下来,您的 user_loader
.从文档中:
Next up, your user_loader
. From the docs:
这将设置用于从会话中重新加载用户的回调.这您设置的函数应采用用户ID(Unicode)并返回用户对象,如果用户不存在,则为None.
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.
这将意味着将您的 user_loader
调整为类似以下内容:
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'
因此,您将您的 login_manager
替换为内容为'info'
的字符串.所以稍后,当您的应用尝试访问 login_manager.user_loader
时,它失败了,因为字符串'info'
没有 user_loader
方法.
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'
这篇关于AttributeError:对象没有属性'user_loader'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!