我试图使用Flask登录管理用户会话,但我一直被困在一个点上。在
login_manager = LoginManager()
login_manager.init_app(app)
class User(UserMixin):
def __init__(self, id, is_active, urole = "admin"):
self.id = id
self.is_active = is_active
self.urole = urole
def is_active(self):
return self.is_active
def activate_user(self):
self.is_active = True
def get_id(self):
return self.id
def get_urole(self):
return self.urole
@login_manager.user_loader
def load_user(user_id):
print(user_id)
return dict_users.get(user_id)
上面是我创建的User
类和load_user
函数。在
现在在我的validate
函数中,我在做这个
logged_in_user.__dict__
的print语句给出了以下输出
{'id': 5, 'is_active': True, 'urole': 'admin'}
(P.S.Am将id硬编码为5,用于测试目的。)
但后来我还是犯了以下错误
line 31, in get_id
return self.id
AttributeError: 'int' object has no attribute 'id'
此外,在load_user(user_id):
函数中,我为user_id
添加了一个print语句,它似乎打印正在添加的用户的id。感觉好像,User
没有被添加到它或类似的东西中,或者它只是以id
即5作为参数。在
我错过了什么?在
编辑:
添加了一个全局指令
global dict_users
dict_users = {}
在登录后更新了main函数中的dict_users
。在
dict_users = logged_in_user
在logout
函数中清空它
@app.route('/logout', methods=['POST', 'GET'])
def logout_dashboard():
if request.method == 'POST':
session['logged_in'] = False
session['username'] = ""
dict_users = {}
return redirect(url_for('login'))
但是现在,每当我试图使用一个带有login_required
的URL时
@app.route('/abc', methods=['GET', 'POST'])
@login_required
def abc():
if request.method == 'POST' or request.method == 'GET':
return render_template('home.html', value=session['username'])
我正在低于错误。在
Unauthorized
The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.
目前没有回答
相关问题 更多 >
编程相关推荐