我有一个使用自己的路由注册的蓝图,因为我需要使用从flask-user库中导入的@roles_required装饰器来验证用户的角色 我可以登录,但是当我需要访问该路由时,出现属性错误:
AttributeError: 'Flask' object has no attribute 'user_manager'
我删除了装饰器,页面呈现得很好。
模型是:
user_roles = db.Table('user_roles',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('role_id', db.Integer, db.ForeignKey('role.id'))
)
class User(CRUDMixin, UserMixin, db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer,
primary_key = True
)
username = db.Column(db.String(64),
index=False,
unique=True,
nullable=False
)
password = db.Column(db.String(64),
index=False,
nullable=False
)
email = db.Column(db.String(80),
index=True,
unique=True,
nullable=False
)
created = db.Column(db.DateTime(timezone=True),
index=False,
unique=False,
nullable=False
)
bio = db.Column(db.Text,
index=False,
unique=False,
nullable=True
)
# Relationsihp with role table
# To get the roles of an user: User.permission.all()
# To get the users of a role: Role.users
roles = db.relationship('Role',
secondary=user_roles,
backref=db.backref('users', lazy='dynamic')
)
def __rep__(self):
return f"<User>('{self.username}', '{self.email}'')"
def __init__(self, name, email, username, password, bio=''):
self.name = name
self.email = email
self.username = username
self.set_password(password)
self.bio = bio
self.created = datetime.datetime.now()
class Role(db.Model):
__tablename__ = 'role'
id = db.Column(db.Integer(),
primary_key=True
)
name = db.Column(db.String(50),
unique=True
)
created = db.Column(db.DateTime(timezone=True),
index=False,
unique=False,
)
def __init__(self, name):
self.name = name
self.created = datetime.datetime.now()
def __repr__(self):
return '<Role {}>'.format(self.name)
路线的定义如下:
from flask_user import roles_required
@myblueprint.route('/')
@roles_required(['admin', 'editor'])
def index_publications():
return "This is my publications page.", 200
正如我所说,对于消息“这是我的出版物页面”,我需要删除装饰器,而从Flask中得到了AttributeError。