主题示例中的MyIndexView错误在Flask-AppBuilder中不起作用

时间:2018-12-26 14:24:38

标签: flask-appbuilder

我从页面https://flask-appbuilder.readthedocs.io/en/latest/customizing.html?highlight=theme的“更改索引”部分开始执行了步骤1、2和3。

我收到以下错误: \ app__init __。py“,第4行     从app.index导入MyIndexView ImportError:无法导入名称“ MyIndexView”

我已经对准系统Flask-AppBuilder应用进行了这些更改。

代码与网站上显示的完全相同。

我希望该示例能够按所述方式工作。但是我在运行时收到了上面发布的消息。

1 个答案:

答案 0 :(得分:0)

您的index.py应该看起来像这样(基本版本)。

# Import flask functions for redirecting and getting user status
from flask import g, url_for, redirect
# Import IndexView class to overwrite files/redirects and expose to expose custom index view
from flask_appbuilder import IndexView, expose

# File to display custom made different views based off if user is signed

class MyIndexView(IndexView):

    # Checking user and redirecting for user when user goes to index view
    @expose('/')
    def index(self):

        # Get user status
        user = g.user

        # Check user
        if user.is_anonymous:
            # user is not authenticated and gets redirected to New user page
            return redirect(url_for('HomeView.new'))
        else:
            # user is authenticated and has an account redirect to General page
            return redirect(url_for('HomeView.general'))

然后在您的views.py中创建一个类似的视图

# Views for any home paths
class HomeView(BaseView):

    # add route base for views as /home
    route_base = "/home"

    # Route for new or logged out users
    @expose('/new')
    def new(self):
        return self.render_template('new_user.py')

    # Route for signed in users or users who want to just view data
    @expose('/general')
    def general(self):
        return self.render_template('my_index.py')

此外,请确保将其添加到您的 init .py

中的appbuilder对象中
appbuilder = AppBuilder(app, db.session, indexview=MyIndexView)