Использую простой код для тестового запуска веб приложения на flask с использованием расширения flask-security. При попытке запусить веб сервер получаю ошибку AttributeError: 'Flask' object has no attribute 'before_first_request'. Использую простой код из примера для быстрого старда
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required
# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
# Bcrypt is set as default SECURITY_PASSWORD_HASH, which requires a salt
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-random-salt'
# Create database connection object
db = SQLAlchemy(app)
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create a user to test with
@app.before_first_request
def create_user():
db.create_all()
user_datastore.create_user(email='matt@nobien.net', password='password')
db.session.commit()
# Views
@app.route('/')
@login_required
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run()
список зависимостей проекта
alembic==1.11.1
Babel==2.12.1
blinker==1.6.2
click==8.1.3
colorama==0.4.6
distlib==0.3.6
dnspython==2.3.0
email-validator==2.0.0.post2
filelock==3.12.2
Flask==2.3.2
Flask-BabelEx==0.9.4
Flask-Login==0.6.2
Flask-Mail==0.9.1
Flask-Migrate==4.0.4
Flask-Principal==0.4.0
Flask-Security==3.0.0
flask-sqlalchemy==3.0.5
Flask-WTF==1.1.1
greenlet==2.0.2
idna==3.4
importlib-metadata==6.7.0
itsdangerous==2.1.2
Jinja2==3.1.2
Mako==1.2.4
markdown-it-py==3.0.0
MarkupSafe==2.1.3
mdurl==0.1.2
packaging==23.1
passlib==1.7.4
pexpect==4.8.0
platformdirs==3.8.0
ptyprocess==0.7.0
Pygments==2.15.1
pytz==2023.3
rich==13.4.2
riot==0.17.3
speaklater==1.3
SQLAlchemy==2.0.17
typing-extensions==4.6.3
virtualenv==20.20.0
Werkzeug==2.3.6
WTForms==3.0.1
zipp==3.15.0
Версия python3.9 ОС Windows 10.