The django-admin-tools dashboard and dashboard modules API¶
This section describe the API of the django-admin-tools dashboard and dashboard modules. Make sure you read this before creating your custom dashboard and custom modules.
- ..note::
If your layout seems to be broken or you have problems with included javascript files, you should try to reset your dashboard preferences (assuming a MySQL backend, the truncate command also works in postgress):
python manage.py dbshell mysql> truncate admin_tools_dashboard_preferences;
For more information see this issue.
The Dashboard
class¶
-
class
admin_tools.dashboard.
Dashboard
(**kwargs)¶ Base class for dashboards. The Dashboard class is a simple python list that has three additional properties:
title
- The dashboard title, by default, it is displayed above the dashboard
in a
h2
tag. Default value: ‘Dashboard’. template
- The template to use to render the dashboard. Default value: ‘admin_tools/dashboard/dashboard.html’
columns
- An integer that represents the number of columns for the dashboard. Default value: 2.
If you want to customize the look of your dashboard and it’s modules, you can declare css stylesheets and/or javascript files to include when rendering the dashboard (these files should be placed in your media path), for example:
from admin_tools.dashboard import Dashboard class MyDashboard(Dashboard): class Media: css = { 'screen, projection': ('css/mydashboard.css',), } js = ('js/mydashboard.js',)
Here’s an example of a custom dashboard:
from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): # we want a 3 columns layout columns = 3 def __init__(self, **kwargs): # append an app list module for "Applications" self.children.append(modules.AppList( title=_('Applications'), exclude=('django.contrib.*',), )) # append an app list module for "Administration" self.children.append(modules.AppList( title=_('Administration'), models=('django.contrib.*',), )) # append a recent actions module self.children.append(modules.RecentActions( title=_('Recent Actions'), limit=5 ))
Below is a screenshot of the resulting dashboard:
-
get_id
()¶ Internal method used to distinguish different dashboards in js code.
-
init_with_context
(context)¶ Sometimes you may need to access context or request variables to build your dashboard, this is what the
init_with_context()
method is for. This method is called just before the display with adjango.template.RequestContext
as unique argument, so you can access to all context variables and to thedjango.http.HttpRequest
.
The AppIndexDashboard
class¶
-
class
admin_tools.dashboard.
AppIndexDashboard
(app_title, models, **kwargs)¶ Class that represents an app index dashboard, app index dashboards are displayed in the applications index page.
AppIndexDashboard
is very similar to theDashboard
class except that its constructor receives two extra arguments:app_title
- The title of the application
models
A list of strings representing the available models for the current application, example:
['yourproject.app.Model1', 'yourproject.app.Model2']
It also provides two helper methods:
get_app_model_classes()
- Method that returns the list of model classes for the current app.
get_app_content_types()
- Method that returns the list of content types for the current app.
If you want to provide custom app index dashboard, be sure to inherit from this class instead of the
Dashboard
class.Here’s an example of a custom app index dashboard:
from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from admin_tools.dashboard import modules, AppIndexDashboard class MyAppIndexDashboard(AppIndexDashboard): # we don't want a title, it's redundant title = '' def __init__(self, app_title, models, **kwargs): AppIndexDashboard.__init__(self, app_title, models, **kwargs) # append a model list module that lists all models # for the app and a recent actions module for the current app self.children += [ modules.ModelList(self.app_title, self.models), modules.RecentActions( include_list=self.models, limit=5 ) ]
Below is a screenshot of the resulting dashboard:
-
get_app_content_types
()¶ Return a list of all content_types for this app.
-
get_app_model_classes
()¶ Helper method that returns a list of model classes for the current app.
-
get_id
()¶ Internal method used to distinguish different dashboards in js code.
The DashboardModule
class¶
-
class
admin_tools.dashboard.modules.
DashboardModule
(title=None, **kwargs)¶ Base class for all dashboard modules. Dashboard modules have the following properties:
enabled
- Boolean that determines whether the module should be enabled in
the dashboard by default or not. Default value:
True
. draggable
- Boolean that determines whether the module can be draggable or not.
Draggable modules can be re-arranged by users. Default value:
True
. collapsible
- Boolean that determines whether the module is collapsible, this
allows users to show/hide module content. Default:
True
. deletable
- Boolean that determines whether the module can be removed from the
dashboard by users or not. Default:
True
. title
- String that contains the module title, make sure you use the django gettext functions if your application is multilingual. Default value: ‘’.
title_url
- String that contains the module title URL. If given the module
title will be a link to this URL. Default value:
None
. css_classes
- A list of css classes to be added to the module
div
class attribute. Default value:None
. pre_content
- Text or HTML content to display above the module content.
Default value:
None
. content
- The module text or HTML content. Default value:
None
. post_content
- Text or HTML content to display under the module content.
Default value:
None
. template
- The template to use to render the module. Default value: ‘admin_tools/dashboard/module.html’.
-
init_with_context
(context)¶ Like for the
Dashboard
class, dashboard modules have ainit_with_context
method that is called with adjango.template.RequestContext
instance as unique argument.This gives you enough flexibility to build complex modules, for example, let’s build a “history” dashboard module, that will list the last ten visited pages:
from admin_tools.dashboard import modules class HistoryDashboardModule(modules.LinkList): title = 'History' def init_with_context(self, context): request = context['request'] # we use sessions to store the visited pages stack history = request.session.get('history', []) for item in history: self.children.append(item) # add the current page to the history history.insert(0, { 'title': context['title'], 'url': request.META['PATH_INFO'] }) if len(history) > 10: history = history[:10] request.session['history'] = history
Here’s a screenshot of our history item:
-
is_empty
()¶ Return True if the module has no content and False otherwise.
>>> mod = DashboardModule() >>> mod.is_empty() True >>> mod.pre_content = 'foo' >>> mod.is_empty() False >>> mod.pre_content = None >>> mod.is_empty() True >>> mod.children.append('foo') >>> mod.is_empty() False >>> mod.children = [] >>> mod.is_empty() True
-
render_css_classes
()¶ Return a string containing the css classes for the module.
>>> mod = DashboardModule(enabled=False, draggable=True, ... collapsible=True, deletable=True) >>> mod.render_css_classes() 'dashboard-module disabled draggable collapsible deletable' >>> mod.css_classes.append('foo') >>> mod.render_css_classes() 'dashboard-module disabled draggable collapsible deletable foo' >>> mod.enabled = True >>> mod.render_css_classes() 'dashboard-module draggable collapsible deletable foo'
The Group
class¶
-
class
admin_tools.dashboard.modules.
Group
(title=None, **kwargs)¶ Represents a group of modules, the group can be displayed in tabs, accordion, or just stacked (default). As well as the
DashboardModule
properties, theGroup
has two extra properties:display
- A string determining how the group should be rendered, this can be one of the following values: ‘tabs’ (default), ‘accordion’ or ‘stacked’.
force_show_title
- Default behaviour for Group module is to force children to always show
the title if Group has
display
=stacked
. If this flag is set toFalse
, children title is shown according to their``show_title`` property. Note that in this case is children responsibility to have meaningful content if no title is shown.
Here’s an example of modules group:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) self.children.append(modules.Group( title="My group", display="tabs", children=[ modules.AppList( title='Administration', models=('django.contrib.*',) ), modules.AppList( title='Applications', exclude=('django.contrib.*',) ) ] ))
The screenshot of what this code produces:
-
is_empty
()¶ A group of modules is considered empty if it has no children or if all its children are empty.
>>> from admin_tools.dashboard.modules import DashboardModule, LinkList >>> mod = Group() >>> mod.is_empty() True >>> mod.children.append(DashboardModule()) >>> mod.is_empty() True >>> mod.children.append(LinkList('links', children=[ ... {'title': 'example1', 'url': 'http://example.com'}, ... {'title': 'example2', 'url': 'http://example.com'}, ... ])) >>> mod.is_empty() False
The LinkList
class¶
-
class
admin_tools.dashboard.modules.
LinkList
(title=None, **kwargs)¶ A module that displays a list of links. As well as the
DashboardModule
properties, theLinkList
takes an extra keyword argument:layout
- The layout of the list, possible values are
stacked
andinline
. The default value isstacked
.
Link list modules children are simple python dictionaries that can have the following keys:
title
- The link title.
url
- The link URL.
external
- Boolean that indicates whether the link is an external one or not.
description
- A string describing the link, it will be the
title
attribute of the htmla
tag. attrs
- Hash comprising attributes of the html
a
tag.
Children can also be iterables (lists or tuples) of length 2, 3, 4 or 5.
Here’s a small example of building a link list module:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) self.children.append(modules.LinkList( layout='inline', children=( { 'title': 'Python website', 'url': 'http://www.python.org', 'external': True, 'description': 'Python language rocks !', 'attrs': {'target': '_blank'}, }, ['Django', 'http://www.djangoproject.com', True], ['Some internal link', '/some/internal/link/'], ) ))
The screenshot of what this code produces:
The AppList
class¶
-
class
admin_tools.dashboard.modules.
AppList
(title=None, **kwargs)¶ Module that lists installed apps and their models. As well as the
DashboardModule
properties, theAppList
has two extra properties:models
- A list of models to include, only models whose name (e.g. “blog.comments.models.Comment”) match one of the strings (e.g. “blog.*”) in the models list will appear in the dashboard module.
exclude
- A list of models to exclude, if a model name (e.g. “blog.comments.models.Comment”) match an element of this list (e.g. “blog.comments.*”) it won’t appear in the dashboard module.
If no models/exclude list is provided, all apps are shown.
Here’s a small example of building an app list module:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) # will only list the django.contrib apps self.children.append(modules.AppList( title='Administration', models=('django.contrib.*',) )) # will list all apps except the django.contrib ones self.children.append(modules.AppList( title='Applications', exclude=('django.contrib.*',) ))
The screenshot of what this code produces:
Note
Note that this module takes into account user permissions, for example, if a user has no rights to change or add a
Group
, then the django.contrib.auth.Group model line will not be displayed.
The ModelList
class¶
-
class
admin_tools.dashboard.modules.
ModelList
(title=None, models=None, exclude=None, **kwargs)¶ Module that lists a set of models. As well as the
DashboardModule
properties, theModelList
takes two extra arguments:models
- A list of models to include, only models whose name (e.g. “blog.comments.models.Comment”) match one of the strings (e.g. “blog.*”) in the models list will appear in the dashboard module.
exclude
- A list of models to exclude, if a model name (e.g. “blog.comments.models.Comment”) match an element of this list (e.g. “blog.comments.*”) it won’t appear in the dashboard module.
Here’s a small example of building a model list module:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) # will only list the django.contrib.auth models self.children += [ modules.ModelList( title='Authentication', models=['django.contrib.auth.*',] ) ]
The screenshot of what this code produces:
Note
Note that this module takes into account user permissions, for example, if a user has no rights to change or add a
Group
, then the django.contrib.auth.Group model line will not be displayed.
The RecentActions
class¶
-
class
admin_tools.dashboard.modules.
RecentActions
(title=None, limit=10, include_list=None, exclude_list=None, **kwargs)¶ Module that lists the recent actions for the current user. As well as the
DashboardModule
properties, theRecentActions
takes three extra keyword arguments:include_list
- A list of contenttypes (e.g. “auth.group” or “sites.site”) to include, only recent actions that match the given contenttypes will be displayed.
exclude_list
- A list of contenttypes (e.g. “auth.group” or “sites.site”) to exclude, recent actions that match the given contenttypes will not be displayed.
limit
- The maximum number of children to display. Default value: 10.
Here’s a small example of building a recent actions module:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) # will only list the django.contrib apps self.children.append(modules.RecentActions( title='Django CMS recent actions', include_list=('cms.page', 'cms.cmsplugin',) ))
The screenshot of what this code produces:
The Feed
class¶
-
class
admin_tools.dashboard.modules.
Feed
(title=None, feed_url=None, limit=None, **kwargs)¶ Class that represents a feed dashboard module.
Important
This class uses the Universal Feed Parser module to parse the feeds, so you’ll need to install it, all feeds supported by FeedParser are thus supported by the Feed
As well as the
DashboardModule
properties, theFeed
takes two extra keyword arguments:feed_url
- The URL of the feed.
limit
- The maximum number of feed children to display. Default value: None, which means that all children are displayed.
Here’s a small example of building a recent actions module:
from admin_tools.dashboard import modules, Dashboard class MyDashboard(Dashboard): def __init__(self, **kwargs): Dashboard.__init__(self, **kwargs) # will only list the django.contrib apps self.children.append(modules.Feed( title=_('Latest Django News'), feed_url='http://www.djangoproject.com/rss/weblog/', limit=5 ))
The screenshot of what this code produces: