Bill
Bill

Reputation: 666

How to change url for Flask-admin

I have a pretty complex Flask site and have added Flask-admin to manage various database tables. It works just fine but I already have an /admin blueprint tree with lots of management routes and templates - e.g. /admin/edit_user

So, I would like to change the flask-admin url from '/admin' to '/dbadmin' to avoid any confusion but setting url= just breaks flask-admin.

this works fine

from flask_admin import Admin    

flaskadmin = Admin(name='My admin')
flaskadmin.add_view(MyModelView(Customer))
flaskadmin.add_view(MyModelView(User))
flaskadmin.init_app(app)

setting url='/dbadmin' stops /admin from working but neither does /dbadmin

flaskadmin = Admin(name='My admin', url='/dbadmin')
flaskadmin.add_view(MyModelView(Customer))
flaskadmin.add_view(MyModelView(User))
flaskadmin.init_app(app)

any ideas how to get this working?

Upvotes: 0

Views: 341

Answers (1)

shinyco
shinyco

Reputation: 1

Refer to this doc: https://flask-admin.readthedocs.io/en/latest/api/mod_base/#default-view

You need to set up an index_view class, you can then set the url to that class.

In your case it might be something like this with flask-login

flaskadmin = Admin(app, name="app", index_view=MyAdminIndexView(url='/dbadmin'))
flaskadmin.add_view(MyModelView(Customer))
flaskadmin.add_view(MyModelView(User))
flaskadmin.init_app(app)

class MyAdminIndexView(AdminIndexView):
    @expose('/')
    def index(self):
        if not login.current_user.is_authenticated:
            return redirect(url_for('.login_view'))
        return super(MyAdminIndexView, self).index()

    @expose('/login/', methods=('GET', 'POST'))
    def login_view(self):
        # handle user login
        form = LoginForm(request.form)
        if helpers.validate_form_on_submit(form):
            user = form.get_user()
            login.login_user(user)

        if login.current_user.is_authenticated:
            return redirect(url_for('.index'))

        link = ''
        self._template_args['form'] = form
        self._template_args['link'] = link
        return super(MyAdminIndexView, self).index()

    @expose('/logout/')
    def logout_view(self):
        login.logout_user()
        return redirect(url_for('.index'))

Upvotes: 0

Related Questions