我正在尝试创建两个行为不同的管理页面。 我已经成功地制作了两个单独的管理页面,但我似乎无法弄清楚如何为第二个管理页面设置AdminIndexView。
class FlaskAdminIndexView(AdminIndexView):
@expose('/')
def index(self):
if not current_user.is_authenticated:
return redirect(url_for('security.login'))
elif (current_user.is_authenticated) and (not current_user.has_role('first_admin')):
return redirect(url_for('post_user.index_entry'))
else:
return super(FlaskAdminIndexView, self).index()
first_admin = flask_admin.Admin(app, 'RCIGM ADMIN', base_template='my_master.html',
template_mode='bootstrap3',
index_view=FlaskAdminIndexView())
以上代码段是我将用户辅助功能设置为第一个管理页面的方式。
class FlaskSecondAdminIndexView(AdminIndexView):
@expose('/')
def index(self):
if not current_user.is_authenticated:
return redirect(url_for('security.login'))
elif (current_user.is_authenticated) and (not current_user.has_role('second_admin')):
return redirect(url_for('post_user.index_entry'))
else:
return self.render('admin/base.html')
#return super(FlaskAdminIndexView, self).index()
second_admin = flask_admin.Admin(app, 'SITE ADMIN', url='/site_admin',
base_template='my_master.html', endpoint='site_admin',
template_mode='bootstrap3', index_view=FlaskSecondAdminIndexView())
上面的代码片段是我的第二个管理页面,我得到的蓝图冲突错误如下所示。
Traceback (most recent call last):
File "/Users/genom003dm/PycharmProjects/sample_accessioning_dev/app/run.py", line 2, in <module>
from app import app
File "/Users/genom003dm/PycharmProjects/sample_accessioning_dev/app/__init__.py", line 55, in <module>
from app.views.post_inputs import post_user_blueprint
File "/Users/genom003dm/PycharmProjects/sample_accessioning_dev/app/views/post_inputs.py", line 65, in <module>
from app.security_layer import generate_registration_token, confirm_token, user_datastore, \
File "/Users/genom003dm/PycharmProjects/sample_accessioning_dev/app/security_layer.py", line 249, in <module>
template_mode='bootstrap3', index_view=FlaskSecondAdminIndexView())
File "/Users/genom003dm/sample_accessioning_dev_virtual_env/lib/python3.5/site-packages/flask_admin/base.py", line 522, in __init__
self._set_admin_index_view(index_view=index_view, endpoint=endpoint, url=url)
File "/Users/genom003dm/sample_accessioning_dev_virtual_env/lib/python3.5/site-packages/flask_admin/base.py", line 566, in _set_admin_index_view
self.add_view(self.index_view)
File "/Users/genom003dm/sample_accessioning_dev_virtual_env/lib/python3.5/site-packages/flask_admin/base.py", line 540, in add_view
self.app.register_blueprint(view.create_blueprint(self))
File "/Users/genom003dm/sample_accessioning_dev_virtual_env/lib/python3.5/site-packages/flask/app.py", line 64, in wrapper_func
return f(self, *args, **kwargs)
File "/Users/genom003dm/sample_accessioning_dev_virtual_env/lib/python3.5/site-packages/flask/app.py", line 946, in register_blueprint
(blueprint, self.blueprints[blueprint.name], blueprint.name)
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x104f95a20> and <flask.blueprints.Blueprint object at 0x104e500b8>. Both share the same name "admin". Blueprints that are created on the fly need unique names.
非常感谢任何帮助或建议!
答案 0 :(得分:0)
创建url
时,您需要传递endpoint
和AdminIndexView
。
例如:
from flask import Flask
from flask_admin import Admin, expose, AdminIndexView
app = Flask(__name__)
@app.route('/')
def index():
return '''
<a href="/first-admin/">Click me to get to First Admin!</a><br>
<a href="/second-admin/">Click me to get to Second Admin!</a>
'''
class FirstAdminIndexView(AdminIndexView):
@expose('/')
def index(self):
return 'Hello From first admin :{}.'.format(self)
class SecondAdminIndexView(AdminIndexView):
@expose('/')
def index(self):
return 'Hello From second admin :{}'.format(self)
first_admin = Admin(
app,
template_mode="bootstrap3",
url='/first-admin',
endpoint='first',
index_view=FirstAdminIndexView(url='/first-admin', endpoint='first')
)
second_admin = Admin(
app,
template_mode="bootstrap3",
url='/second-admin',
endpoint='second',
index_view=SecondAdminIndexView(url='/second-admin', endpoint='second')
)
if __name__ == '__main__':
app.run(port=5000, debug=True)