flask_admin.base¶
Admin¶
- class Admin(app: Flask | None = None, name: str | None = None, url: str | None = None, subdomain: str | None = None, index_view: AdminIndexView | None = None, translations_path: str | None = None, endpoint: str | None = None, static_url_path: str | None = None, theme: Theme | None = None, category_icon_classes: dict[str, str] | None = None, host: str | None = None, csp_nonce_generator: Callable[[], Any] | None = None)[source]¶
Collection of the admin views. Also manages menu structure.
Constructor.
- Parameters:
app – Flask application object
name – Application name. Will be displayed in the main menu and as a page title. Defaults to “Admin”
url – Base URL
subdomain – Subdomain to use
index_view – Home page view to use. Defaults to AdminIndexView.
translations_path – Location of the translation message catalogs. By default will use the translations shipped with Flask-Admin.
endpoint – Base endpoint name for index view. If you use multiple instances of the Admin class with a single Flask application, you have to set a unique endpoint name for each instance.
static_url_path – Static URL Path. If provided, this specifies the default path to the static url directory for all its views. Can be overridden in view configuration.
theme – Base theme. Defaults to Bootstrap4Theme().
category_icon_classes – A dict of category names as keys and html classes as values to be added to menu category icons. Example: {‘Favorites’: ‘glyphicon glyphicon-star’}
host – The host to register all admin views on. Mutually exclusive with subdomain
csp_nonce_generator – A callable that returns a nonce to inject into Flask-Admin JS, CSS, etc.
- add_category(name: str, class_name: str | None = None, icon_type: str | None = None, icon_value: str | None = None) 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(link: MenuLink) None[source]¶
Add link to menu links collection.
- Parameters:
link – Link to add.
- add_links(*args: MenuLink) None[source]¶
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 to menu tree hierarchy.
- Parameters:
menu_item – MenuItem class instance
target_category – Target category name
- add_sub_category(name: str, parent_name: str) None[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: BaseView) None[source]¶
Add a view to the collection.
- Parameters:
view – View to add.
- add_views(*args: Any) None[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: Flask, index_view: AdminIndexView | None = None, endpoint: str | None = None, url: str | None = None) None[source]¶
Register all views with the Flask application.
Return the menu hierarchy.
Return menu links.
Base View¶
- expose(url: str = '/', methods: Iterable[str] | None = ('GET',)) Callable[[Any], Any][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: str = '/') Callable[[Any], Any][source]¶
Decorator to expose Flask’s pluggable view classes (
flask.views.Vieworflask.views.MethodView).- Parameters:
url – Relative URL for the view
Added in version 1.0.4.
- class BaseView(name: str | None = None, category: str | None = None, endpoint: str | None = None, url: str | None = None, static_folder: str | None = None, static_url_path: str | None = None, menu_class_name: str | None = None, menu_icon_type: str | None = None, menu_icon_value: str | None = 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' ) )
Constructor.
- Parameters:
name – Name of this view. If not provided, will default to the class name.
category – View category. If not provided, this view will be shown as a top-level menu item. Otherwise, it will be in a submenu.
endpoint – Base endpoint name for the view. For example, if there’s a view method called “index” and endpoint is set to “myadmin”, you can use url_for(‘myadmin.index’) to get the URL to the view method. Defaults to the class name in lower case.
url – Base URL. If provided, affects how URLs are generated. For example, if the url parameter is “test”, the resulting URL will look like “/admin/test/”. If not provided, will use endpoint as a base url. However, if URL starts with ‘/’, absolute path is assumed and ‘/admin/’ prefix won’t be applied.
static_url_path – Static URL Path. If provided, this specifies the path to the static url directory.
menu_class_name – Optional class name for the menu item.
menu_icon_type –
Optional icon. Possible icon types:
flask_admin.consts.ICON_TYPE_GLYPH - Bootstrap glyph icon
flask_admin.consts.ICON_TYPE_FONT_AWESOME - Font Awesome icon
- flask_admin.consts.ICON_TYPE_IMAGE - Image relative to Flask static
directory
flask_admin.consts.ICON_TYPE_IMAGE_URL - Image with full URL
menu_icon_value – Icon glyph name or URL, depending on menu_icon_type setting
- get_url(endpoint: str, **kwargs: Any) str[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: Any, **kwargs: Any) Any[source]¶
Handle the response to inaccessible views.
By default, it throw HTTP 403 error. Override this method to customize the behaviour.
- is_accessible() bool[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.
Default (Index) view¶
- class AdminIndexView(name: str | None = None, category: str | None = None, endpoint: str | None = None, url: str | None = None, template: str = 'admin/index.html', menu_class_name: str | None = None, menu_icon_type: str | None = None, menu_icon_value: str | None = None)[source]¶
Default administrative interface index page when visiting the
/admin/URL.It can be overridden by passing your own view class to the
Adminconstructor: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
adminDefault URL route is
/admin.Automatically associates with static folder.
Default template is
admin/index.html
Constructor.
- Parameters:
name – Name of this view. If not provided, will default to the class name.
category – View category. If not provided, this view will be shown as a top-level menu item. Otherwise, it will be in a submenu.
endpoint – Base endpoint name for the view. For example, if there’s a view method called “index” and endpoint is set to “myadmin”, you can use url_for(‘myadmin.index’) to get the URL to the view method. Defaults to the class name in lower case.
url – Base URL. If provided, affects how URLs are generated. For example, if the url parameter is “test”, the resulting URL will look like “/admin/test/”. If not provided, will use endpoint as a base url. However, if URL starts with ‘/’, absolute path is assumed and ‘/admin/’ prefix won’t be applied.
static_url_path – Static URL Path. If provided, this specifies the path to the static url directory.
menu_class_name – Optional class name for the menu item.
menu_icon_type –
Optional icon. Possible icon types:
flask_admin.consts.ICON_TYPE_GLYPH - Bootstrap glyph icon
flask_admin.consts.ICON_TYPE_FONT_AWESOME - Font Awesome icon
- flask_admin.consts.ICON_TYPE_IMAGE - Image relative to Flask static
directory
flask_admin.consts.ICON_TYPE_IMAGE_URL - Image with full URL
menu_icon_value – Icon glyph name or URL, depending on menu_icon_type setting