您的 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)