How to reference a ModelView in flask-admin
Asked Answered
P

3

7

What's the right way to get the URL for a flask-admin ModelView?

Here's a very simple example:

my_admin_view.py

from flask.ext.admin.contrib.sqla import ModelView
from common.flask_app import app
from models import db, User, Role

admin = Admin(app, name="Boost Admin")

admin.add_view(ModelView(User,  db.session, category="model"))
admin.add_view(ModelView(Role,  db.session, category="model"))

my_admin_template.html

...
<p>Check out my user admin link:</p>

<a href="{{ url_for('modelview.user') }}">User view link</a>
{#                   ______________ 
                     what argument to pass in here??      #}
...

What's the correct argument to pass to url_for(...)?

I've tried modelview.user, my_admin_view.modelview.user, etc. None of them seem to resolve correctly, and I'd like to avoid hardcoding the link.

thanks!

Primogenitor answered 6/12, 2013 at 19:0 Comment(0)
P
12

OK I figured it out after reading the source code for ModelView.

First, make sure that endpoints are named (it's possible to do it without named endpoints, but this makes the code much clearer):

from flask.ext.admin.contrib.sqla import ModelView
from models import db, User, Role

admin = Admin(app, name="Boost Admin")

admin.add_view(ModelView(User,db.session,category="model", endpoint="model_view_user"))
admin.add_view(ModelView(Role,db.session,category="model", endpoint="model_view_role"))

...now in the template, you can reference the basic model view as follows:

URL for User model default view is: {{model_view_user.index_view}} 
URL for Role model default view is: {{model_view_role.index_view}} 

The index_view function is defined here, and implements the default view for a flask admin ModelView.

Primogenitor answered 1/2, 2014 at 1:2 Comment(1)
If you don't provide an endpoint, the default is (model_name+'view').lower().Gee
S
5

See the section Generating URLs in the Flask-Admin introduction.

It says to "use the lowercase name of the model as the prefix". Add a dot, and the name of the view.

  • index_view for the overview list.
  • create_view for creating a new row.
  • edit_view for modifying an existing row.

So you can easily do:

url_for('user.index_view')
url_for('role.create_view')
url_for('user.edit_view', id=1)
Sandalwood answered 1/3, 2016 at 10:37 Comment(0)
S
0

It should be

url_for('admin.user')

If you read the flask-admin docs here, for generating URLs, it clearly says:

If you want to generate a URL for a particular view method from outside, the following rules apply:

....

3. For model-based views the rules differ - the model class name should be used if an endpoint name is not provided. 
Syncopated answered 6/12, 2013 at 19:10 Comment(3)
thanks for the quick response. I've tried admin.user, admin.User, my_admin_view.user, and my_admin_view.User ....they all raise a routing error. (BuildError: {'admin.user',{}, None})Primogenitor
Where is your admin template stored ? can you share the structure of the project ?Syncopated
The project generally conforms to the Flask tutorial structure. The admin template is stored in a directory structure under /templates/admin which mirrors the flask-admin distribution package templates. I have verified that the directory successfully overrides the default flask-admin templates (e.g. by temporarily adding some HTML to admin/base.html to make sure).Primogenitor

© 2022 - 2024 — McMap. All rights reserved.