flask登录时出错 [英] Error with flask-login
问题描述
我很难使用Flask-Login框架进行身份验证.我已经尽可能详尽地浏览了文档,但显然我缺少明显的内容.
I have difficulty in using the Flask-Login framework for authentication. I have looked through the documentation as thoroughly as possible but apparently I am missing something obvious.
class User():
def __init__(self, userid=None, username=None, password=None):
self.userid = userid
self.username = username
self.password = password
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.userid)
def __repr__(self):
return '<User %r>' % self.username
def find_by_username(username):
try:
data = app.mongo.db.users.find_one_or_404({'username': username})
user = User()
user.userid = data['_id']
user.username = data['username']
user.password = data['password']
return user
except HTTPException:
return None
def find_by_id(userid):
try:
data = app.mongo.db.users.find_one_or_404({'_id': userid})
user = User(data['_id'], data['username'], data['password'])
return user
except HTTPException:
return None
上面是我的User类,位于 users/models.py
The above is my User class located in users/models.py
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'users.login'
@login_manager.user_loader
def load_user(userid):
return find_by_id(userid)
以上是我的用户加载程序.
The above is my user loader.
@mod.route('/login/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
pw_hash = hashlib.md5(form.password.data).hexdigest()
user = find_by_username(form.username.data)
if user is not None:
if user.password == pw_hash:
if login_user(user):
flash('Logged in successfully.')
return redirect(request.args.get('next') or url_for('users.test'))
else:
flash('Error')
else:
flash('Username or password incorrect')
else:
flash('Username or password incorrect')
return render_template('users/login.html', form=form)
显然没有错误消息,但是当尝试访问任何用 @login_required
装饰的视图时,它会将我重定向到登录表单.据我所知, login_user
函数实际上并不起作用,尽管在我调用它时会返回 True
.任何建议表示赞赏.
There is no apparently error message, but when trying to access any views decorated with @login_required
, it redirects me to the login form. Best as I can tell, the login_user
function isn't actually working although it returns True
when I called it. Any advice appreciated.
推荐答案
在调试器中调试了一段时间之后,我终于解决了这个问题.
After stepping through a debugger for a while, I finally fixed the problem.
关键问题是我试图将MongoDB集合中的 _id
参数用作用户ID.我没有意识到 _id
参数是 ObjectID
类型,而不是我需要的字符串或unicode.
The key issue is that I was attempting to use the _id
parameter from the MongoDB collection as the userid. I did not realize that the _id
parameter was an ObjectID
type instead of a string or unicode which I needed.
def find_by_username(username):
try:
data = app.mongo.db.users.find_one_or_404({'username': username})
user = User(unicode(data['_id']), data['username'], data['password'])
return user
except HTTPException:
return None
def find_by_id(userid):
try:
data = app.mongo.db.users.find_one_or_404({'_id': ObjectId(userid)})
user = User(unicode(data['_id']), data['username'], data['password'])
return user
修改这两个函数可以适当地修复此错误.
Modifying the two functions appropriately fixed this error.
这篇关于flask登录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!