将flask-admin添加到项目时错误的仪表板

时间:2018-04-27 22:20:24

标签: python flask flask-admin

enter image description here

我试图扩展基于烧瓶的项目https://github.com/hack4impact/flask-base/tree/master/app。这使用了app / init.py和蓝图中的应用程序工厂模式。

在app / init.py中我有:

import os
from flask import Flask
from flask_mail import Mail
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_assets import Environment
from flask_wtf import CsrfProtect
from flask_compress import Compress
from flask_rq import RQ
from flask_admin import Admin, BaseView, expose
from flask_admin.contrib.sqla import ModelView
# from app.models import User


from config import config
from .assets import app_css, app_js, vendor_css, vendor_js




basedir = os.path.abspath(os.path.dirname(__file__))

mail = Mail()
db = SQLAlchemy()
csrf = CsrfProtect()
compress = Compress()


# Set up Flask-Login
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'account.login'

from app.models import User


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # not using sqlalchemy event system, hence disabling it


    with app.app_context():
        m =app.url_map

    config[config_name].init_app(app)

    # Set up extensions
    mail.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)
    csrf.init_app(app)
    compress.init_app(app)
    RQ(app)
    # adm = Admin(app, name='MyAPP')
    adm = Admin(endpoint='adminz', name='adz', url='/adminz')



   ......

    # Create app blueprints
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .account import account as account_blueprint
    app.register_blueprint(account_blueprint, url_prefix='/account')

    from .admin import admin as admin_blueprint
    # from .admin import admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')

    return app

模板/管理/ db.html:

<p>Hello world</p>

对于管理员观看次数(https://github.com/hack4impact/flask-base/blob/master/app/admin/views.py),我已添加:

from flask_admin import Admin, BaseView, expose
from flask_admin.contrib.sqla import ModelView
from app import adm as adm, db

class MyView(ModelView):
    @expose('/')
    def db(self):
        return self.render('admin/db.html')


# admin management setup
@main.route('/db')
def db():
    adm.add_view(MyView(User, db.session))

当我打开时,我的管理信息中心不是刻录管理员基本视图:

 127.0.0.1:5000/db

enter image description here

我做错了什么?

编辑:

按照您的指示,我将create_app更改为以:

开头
def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # not using sqlalchemy event system, hence disabling it
    # adm = Admin(name='admin2', endpoint='/db', url='/db', template_mode='bootstrap3',base_template='admin/db.html')


    config[config_name].init_app(app)

    # Set up extensions
    mail.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)
    csrf.init_app(app)
    compress.init_app(app)
    RQ(app)
    adm = Admin(app, name='MyAPP')
    # adm = Admin(endpoint='adminz', name='adz', url='/adminz')
    adm.add_view(MyView(User, db.session, endpoint='db'))

这导致:

文件&#34; .... flask \ app.py&#34;,第946行,在register_blueprint中     (blueprint,self.blueprints [blueprint.name],blueprint.name) 断言错误:和之间发生了蓝图的名称冲突。两者都使用相同的名称&#34; admin&#34;。动态创建的蓝图需要唯一的名称。

EDIT2:

到create_app的末尾我已添加:

# Create app blueprints
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)

from .account import account as account_blueprint
app.register_blueprint(account_blueprint, url_prefix='/account')

from .admin import admin as admin_blueprint
# from .admin import admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin')
# app.register_blueprint(admin_blueprint, url_prefix='/ab')

with app.app_context():
    m =app.url_map

return app

我不确定你想看到什么,但是m.rules给出了:

 <Rule '/account/manage/change-password' (HEAD, GET, OPTIONS, POST) -> account.change_password>,
 <Rule '/account/manage/change-email' (HEAD, GET, OPTIONS, POST) -> account.change_email_request>,
 <Rule '/account/manage/info' (HEAD, GET, OPTIONS, POST) -> account.manage>,
 <Rule '/account/confirm-account' (HEAD, GET, OPTIONS) -> account.confirm_request>,
 <Rule '/account/reset-password' (HEAD, GET, OPTIONS, POST) -> account.reset_password_request>,
 <Rule '/account/unconfirmed' (HEAD, GET, OPTIONS) -> account.unconfirmed>,
 <Rule '/account/register' (HEAD, GET, OPTIONS, POST) -> account.register>,
 <Rule '/account/logout' (HEAD, GET, OPTIONS) -> account.logout>,
 <Rule '/account/manage' (HEAD, GET, OPTIONS, POST) -> account.manage>,
 <Rule '/account/login' (HEAD, GET, OPTIONS, POST) -> account.login>,
 <Rule '/admin/_update_editor_contents' (OPTIONS, POST) -> admin.update_editor_contents>,
 <Rule '/admin/invite-user' (HEAD, GET, OPTIONS, POST) -> admin.invite_user>,
 <Rule '/admin/new-user' (HEAD, GET, OPTIONS, POST) -> admin.new_user>,
 <Rule '/admin/users' (HEAD, GET, OPTIONS) -> admin.registered_users>,
 <Rule '/get_session_job_value' (HEAD, GET, OPTIONS) -> main.get_session_job_value>,
 <Rule '/cl_confirm_chrome' (HEAD, GET, OPTIONS, POST) -> main.cl_confirm_chrome>,
 <Rule '/render_png' (HEAD, GET, OPTIONS) -> main.render_png>,
 <Rule '/selected' (HEAD, GET, OPTIONS) -> main.selected>,
 <Rule '/cl_dash' (HEAD, GET, OPTIONS, POST) -> main.cl_dash>,
 <Rule '/about' (HEAD, GET, OPTIONS) -> main.about>,
 <Rule '/admin/' (HEAD, GET, OPTIONS) -> admin.index>,
 <Rule '/dash' (HEAD, GET, OPTIONS) -> main.dash>,
 <Rule '/jobs' (HEAD, GET, OPTIONS) -> main.get_jobs>,
 <Rule '/' (HEAD, GET, OPTIONS) -> main.index>,
 <Rule '/account/manage/change-email/<token>' (HEAD, GET, OPTIONS, POST) -> account.change_email>,
 <Rule '/admin/user/<user_id>/change-account-type' (HEAD, GET, OPTIONS, POST) -> admin.change_account_type>,
 <Rule '/admin/user/<user_id>/change-email' (HEAD, GET, OPTIONS, POST) -> admin.change_user_email>,
 <Rule '/admin/user/<user_id>/_delete' (HEAD, GET, OPTIONS) -> admin.delete_user>,
 <Rule '/admin/user/<user_id>/delete' (HEAD, GET, OPTIONS) -> admin.delete_user_request>,
 <Rule '/admin/user/<user_id>/info' (HEAD, GET, OPTIONS) -> admin.user_info>,
 <Rule '/account/join-from-invite/<user_id>/<token>' (HEAD, GET, OPTIONS, POST) -> account.join_from_invite>,
 <Rule '/account/confirm-account/<token>' (HEAD, GET, OPTIONS) -> account.confirm>,
 <Rule '/account/reset-password/<token>' (HEAD, GET, OPTIONS, POST) -> account.reset_password>,
 <Rule '/admin/static/<filename>' (HEAD, GET, OPTIONS) -> admin.static>,
 <Rule '/admin/user/<user_id>' (HEAD, GET, OPTIONS) -> admin.user_info>,
 <Rule '/results/<job_key>' (HEAD, GET, OPTIONS) -> main.get_results>,
 <Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>

编辑3:

我不得不说这是一个不可思议的答案!你真的教过我很多东西。我已按照您的指示更换了上述规则的网址。我10天前的原始计划只是使用基本的flask-admin CRUD功能。我对db.html模板不感兴趣(这只是我尝试过的)。

无论如何都试图更改管理蓝图的名称(您的号码1)。我在尝试将app / admin / init.py更改为:

之前尝试过此操作
from flask import Blueprint
admin = Blueprint('admin_blueprint', __name__)
from . import views  # noqa

现在我打开

http://127.0.0.1:5000/adminz/

我收到404错误

最终编辑:

问题由https://chat.stackoverflow.com/users/5819113/diego-quintana解决,他解释说创建蓝图的flask-admin与基于烧瓶的管理蓝图之间存在冲突。通过更改蓝图的名称和基于flask的项目的静态文件夹。 Flask-admin可以在不被覆盖的情况下工作。请参阅https://github.com/kc1/flask-base

1 个答案:

答案 0 :(得分:2)

使用-o代替url

endpoint
相关问题