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:
all_rdps
?url_for
to generate urls for these endpoints? url_for('admin.All_RDPs.testindex')
, url_for('admin.All_RDPs')
don't work.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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With