Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 with Flask-Admin like so:

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

and then is viewable from the browser like so:

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

the question is:

  1. how do I specify the URL for this class instead of the default generated name all_rdps?
  2. how do I use url_for to generate urls for these endpoints? url_for('admin.All_RDPs.testindex'), url_for('admin.All_RDPs') don't work.
like image 958
kurtgn Avatar asked Nov 11 '15 12:11

kurtgn


People also ask

What is url_for function in flask?

The url_for() function is used to build a URL to the specific function dynamically. The first argument is the name of the specified function, and then we can pass any number of keyword argument corresponding to the variable part of the URL.

What is the purpose of the url_for function?

The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL.

How do I create an administrative interface in flask?

It's very easy to use flask-admin, just import the Admin class, create a new object with it, and pass in our flask application object as the first parameter, the name argument is the name that's displayed on the admin homepage, it defaults to the application name.

Does flask have admin?

It offers freedom for you, the designer, to implement your project in a way that suits your particular application. Why Flask-Admin? In a world of micro-services and APIs, Flask-Admin solves the boring problem of building an admin interface on top of an existing data model.


1 Answers

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

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

In this case, you can generate links by concatenating the view method name with an endpoint:

url_for('testadmin.index')

If you don't override the endpoint name, the lower-case class name can be used for generating URLs, like in:

url_for('myview.index')

For model-based views the rules differ - the model class name should be used if an endpoint name is not provided. The ModelView also has these endpoints by default: .index_view, .create_view, and .edit_view. So, the following urls can be generated for a model named "User":

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

Source: Flask-Admin quickstart

like image 91
mr.wolle Avatar answered Sep 22 '22 14:09

mr.wolle