Url_for for class-based views in Flask-Admin

I have a class based admin view:

class All_RDPs(BaseView):
    @expose('/')
    def index(self):
        return 'ok1'
    @expose('/test')
    def testindex(self):
        return 'ok2'

which is registered in Flask-Admin, for example:

admin.add_view(All_RDPs(name='dep_rdp'))

and then displayed in the browser like this:

http://localhost/admin/all_rdps/
http://localhost/admin/all_rdps/test

the question arises:

  • how to specify the url of this class instead of the default generated name all_rdps?
  • How to use url_forto generate URLs for these endpoints? url_for('admin.All_RDPs.testindex'), url_for('admin.All_RDPs')Do not work.
+4
source share
1 answer

You can override the endpoint name by passing the endpoint parameter to the view class constructor:

admin = Admin(app)
admin.add_view(MyView(endpoint='testadmin'))

In this case, you can create links by combining the name submission method with the endpoint:

url_for('testadmin.index')

, URL-, :

url_for('myview.index')

, , - , . ModelView :.index_view,.create_view .edit_view. , "" URL-:

# List View
url_for('user.index_view')

# Create View (redirect back to index_view)
url_for('user.create_view', url=url_for('user.index_view'))

# Edit View for record #1 (redirect back to index_view)
url_for('user.edit_view', id=1, url=url_for('user.index_view'))

: Flask-Admin

+3

Source: https://habr.com/ru/post/1615407/


All Articles