1

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

我收到以下错误:\app__init__.py", line 4, in from app.index import MyIndexView ImportError: cannot import name 'MyIndexView'

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

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

我希望这个例子能像描述的那样工作。但是当我运行它时,我收到了上面发布的消息。

4

1 回答 1

-1

您的 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中创建一个像这样的simle视图

# 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)
于 2019-01-17T19:49:16.963 回答