1

I implemented steps 1, 2 and 3 from the "Changing the index" section of this page https://flask-appbuilder.readthedocs.io/en/latest/customizing.html?highlight=theme.

I get the following error: \app__init__.py", line 4, in from app.index import MyIndexView ImportError: cannot import name 'MyIndexView'

I have made these changes to a barebone Flask-AppBuilder app.

The code is exactly as is shown on the site.

I expect the example to work as described. But I receive the message I posted above when I run it.

1 Answers1

-1

Your index.py should look like this(base version).

# 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'))

Then in your views.py create a simle view like this

# 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')

Also, make sure to add it to your appbuilder object in your init.py

appbuilder = AppBuilder(app, db.session, indexview=MyIndexView)