flask_admin.base

Base View

expose(url='/', methods=('GET', ))[source]

Use this decorator to expose views in your view classes.

Parameters:
  • url – Relative URL for the view
  • methods – Allowed HTTP methods. By default only GET is allowed.
expose_plugview(url='/')[source]

Decorator to expose Flask’s pluggable view classes (flask.views.View or flask.views.MethodView).

Parameters:url – Relative URL for the view

New in version 1.0.4.

class BaseView(name=None, category=None, endpoint=None, url=None, static_folder=None, static_url_path=None, menu_class_name=None, menu_icon_type=None, menu_icon_value=None)[source]

Base administrative view.

Derive from this class to implement your administrative interface piece. For example:

from flask_admin import BaseView, expose
class MyView(BaseView):
    @expose('/')
    def index(self):
        return 'Hello World!'

Icons can be added to the menu by using menu_icon_type and menu_icon_value. For example:

admin.add_view(MyView(name='My View', menu_icon_type='glyph', menu_icon_value='glyphicon-home'))
create_blueprint(admin)[source]

Create Flask blueprint.

get_url(endpoint, **kwargs)[source]

Generate URL for the endpoint. If you want to customize URL generation logic (persist some query string argument, for example), this is right place to do it.

Parameters:
  • endpoint – Flask endpoint name
  • kwargs – Arguments for url_for
inaccessible_callback(name, **kwargs)[source]

Handle the response to inaccessible views.

By default, it throw HTTP 403 error. Override this method to customize the behaviour.

is_accessible()[source]

Override this method to add permission checks.

Flask-Admin does not make any assumptions about the authentication system used in your application, so it is up to you to implement it.

By default, it will allow access for everyone.

is_visible()[source]

Override this method if you want dynamically hide or show administrative views from Flask-Admin menu structure

By default, item is visible in menu.

Please note that item should be both visible and accessible to be displayed in menu.

render(template, **kwargs)[source]

Render template

Parameters:
  • template – Template path to render
  • kwargs – Template arguments

Default view

class AdminIndexView(name=None, category=None, endpoint=None, url=None, template='admin/index.html', menu_class_name=None, menu_icon_type=None, menu_icon_value=None)[source]

Default administrative interface index page when visiting the /admin/ URL.

It can be overridden by passing your own view class to the Admin constructor:

class MyHomeView(AdminIndexView):
    @expose('/')
    def index(self):
        arg1 = 'Hello'
        return self.render('admin/myhome.html', arg1=arg1)

admin = Admin(index_view=MyHomeView())

Also, you can change the root url from /admin to / with the following:

admin = Admin(
    app,
    index_view=AdminIndexView(
        name='Home',
        template='admin/myhome.html',
        url='/'
    )
)

Default values for the index page are:

  • If a name is not provided, ‘Home’ will be used.
  • If an endpoint is not provided, will default to admin
  • Default URL route is /admin.
  • Automatically associates with static folder.
  • Default template is admin/index.html

Admin

class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=None, endpoint=None, static_url_path=None, base_template=None, template_mode=None, category_icon_classes=None)[source]

Collection of the admin views. Also manages menu structure.

add_category(name, class_name=None, icon_type=None, icon_value=None)[source]

Add a category of a given name

Parameters:
  • name – The name of the new menu category.
  • class_name – The class name for the new menu category.
  • icon_type – The icon name for the new menu category.
  • icon_value – The icon value for the new menu category.

Add link to menu links collection.

Parameters:link – Link to add.

Add one or more links to the menu links collection.

Examples:

admin.add_links(link1)
admin.add_links(link1, link2, link3, link4)
admin.add_links(*my_list)
Parameters:args – Argument list including the links to add.
add_menu_item(menu_item, target_category=None)[source]

Add menu item to menu tree hierarchy.

Parameters:
  • menu_item – MenuItem class instance
  • target_category – Target category name
add_sub_category(name, parent_name)[source]

Add a category of a given name underneath the category with parent_name.

Parameters:
  • name – The name of the new menu category.
  • parent_name – The name of a parent_name category
add_view(view)[source]

Add a view to the collection.

Parameters:view – View to add.
add_views(*args)[source]

Add one or more views to the collection.

Examples:

admin.add_views(view1)
admin.add_views(view1, view2, view3, view4)
admin.add_views(*my_list)
Parameters:args – Argument list including the views to add.
init_app(app, index_view=None, endpoint=None, url=None)[source]

Register all views with the Flask application.

Parameters:app – Flask application instance
menu()[source]

Return the menu hierarchy.

Return menu links.