@Insssane

При запуске приложения на Flask, безконечно загружаются все страницы, с чем это связано, и как решить?

Всем доброго времни суток! Столкнулся с такой проблемой, что при переходе по /admin, /registration, /login и главная страничка постоянно грузятся и ничего не происходит.
Код:

app.py
from flask import Flask, request, redirect, url_for
from config import Configuration

from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_admin import AdminIndexView


from flask_login import current_user

from flask_security import Security, SQLAlchemyUserDatastore

from models import *


def create_app():
    app = Flask(__name__)
    app.config.from_object(Configuration)
    db.init_app(app)
    return app


app = create_app()
app.app_context().push()

db.create_all()


# flask-security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# admin user
user_datastore.create_user(email='admin4@local.com', password='admin')
user = User.query.first()

user_datastore.create_role(name='admin', description='administrator')

role = Role.query.first()
user_datastore.add_role_to_user(user, role)

# ADMIN


class AdminView(ModelView):
    def is_accessible(self):
        return current_user.has_role('admin')

    def inaccessible_callback(self, name, **kwargs):
        return redirect(url_for('security.login', next=request.url))


class HomeAdminView(AdminIndexView):
    def is_accessible(self):
        return current_user.has_role('admin')

    def inaccessible_callback(self, name, **kwargs):
        return redirect(url_for('security.login', next=request.url))


admin = Admin(app, 'FlaskApp', url='/admin', index_view=HomeAdminView(name='Home'))
admin.add_view(AdminView(Author, db.session))
admin.add_view(AdminView(Book, db.session))
admin.add_view(AdminView(User, db.session))
admin.add_view(AdminView(Role, db.session))


models.py
from flask_security import UserMixin, RoleMixin

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class Author(db.Model):
    __tablename__ = 'author'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    birthday = db.Column(db.Date)
    name = db.Column(db.String(32), unique=True)

    def __init__(self, id, birthday, name):
        self.id = id
        self.name = name
        self.birthday = birthday

    def __repr__(self, *args, **kwargs):
        return f'{self.id}.{self.name}'


class Book(db.Model):
    __tablename__ = 'book'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64))
    published = db.Column(db.Float)
    is_published = db.Column(db.Boolean, default=True)

    def __init__(self, id, title, published, is_published):
        self.id = id
        self.title = title
        self.published = published
        self.is_published = is_published

    def __repr__(self, *args, **kwargs):
        return f'{self.id}.{self.title}'

    book_name = db.Column(db.String(32), db.ForeignKey('author.name'))
    name = db.relationship('Author', backref=db.backref('book', lazy=True))


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 User(db.Model, UserMixin):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), 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'))


class Role(db.Model, RoleMixin):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    description = db.Column(db.String(255))


create_admin.py
from app import db
from app import user_datastore
from app import app

from models import User, Role



if __name__ == '__main__':
    email = 'admin@admin.ru'
    password = 'admin'

    user_datastore.create_user(email=email, password=password)
    db.session.commit()

    user_datastore.create_role(name='admin', description='administrator')
    db.session.commit()

    admin_user = User.query.filter_by(email=email).first()
    admin_role = Role.query.filter_by(name='admin').first()

    user_datastore.add_role_to_user(admin_user, admin_role)
    db.session.commit()

    print('Admin user created.\nE-mail: {}\nPassword: {}'.format(email, password))


config.py
class Configuration(object):
    SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://userr:password@localhost/flaskdata'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    FLASK_APP = "webapp"
    DEBUG = True


# Flask-Security
    SECRET_KEY = 'mysecret'
    SECURITY_PASSWORD_SALT = 'mysecretsalt'
    SECURITY_REGISTERABLE = True
    SECURITY_SEND_REGISTER_EMAIL = False
    SECURITY_RECOVERABLE = True
    SECURITY_CHANGEABLE = True


main.py
from app import app

if __name__ == '__main__':
    app.run(debug=True)


view.py
from app import *

from flask import render_template
from flask import request

from flask_security.utils import hash_password


@app.route('/')
def index():
    return render_template('index.html')
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы