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.
Base class for dashboards. The Dashboard class is a simple python list that has three additional properties:
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, for example:
from admin_tools.dashboard import Dashboard
class MyDashboard(Dashboard):
class Media:
css = ('/media/css/mydashboard.css',)
js = ('/media/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):
def __init__(self, **kwargs):
# we want a 3 columns layout
self.columns = 3
# append an app list module for "Applications"
self.children.append(modules.AppList(
title=_('Applications'),
exclude_list=('django.contrib',),
))
# append an app list module for "Administration"
self.children.append(modules.AppList(
title=_('Administration'),
include_list=('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:
Internal method used to distinguish different dashboards in js code.
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 a django.template.RequestContext as unique argument, so you can access to all context variables and to the django.http.HttpRequest.
Class that represents an app index dashboard, app index dashboards are displayed in the applications index page. AppIndexDashboard is very similar to the Dashboard class except that its constructor receives two extra arguments:
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:
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):
def __init__(self, **kwargs):
AppIndexDashboard.__init__(self, **kwargs)
# we don't want a title, it's redundant
self.title = ''
# append a model list module that lists all models
# for the app
self.children.append(modules.ModelList(
title=self.app_title,
include_list=self.models,
))
# append a recent actions module for the current app
self.children.append(modules.RecentActions(
title=_('Recent Actions'),
include_list=self.models,
limit=5
))
Below is a screenshot of the resulting dashboard:
Return a list of all content_types for this app.
Helper method that returns a list of model classes for the current app.
Internal method used to distinguish different dashboards in js code.
Base class for all dashboard modules. Dashboard modules have the following properties:
Like for the Dashboard class, dashboard modules have a init_with_context method that is called with a django.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):
def init_with_context(self, context):
self.title = 'History'
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:
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
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'
Represents a group of modules, the group can be displayed in tabs, accordion, or just stacked (default). As well as the DashboardModule properties, the AppList has one extra property:
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',
include_list=('django.contrib',)
),
modules.AppList(
title='Applications',
exclude_list=('django.contrib',)
)
]
))
The screenshot of what this code produces:
A module that displays a list of links. As well as the DashboardModule properties, the LinkList takes an extra keyword argument:
Link list modules children are simple python dictionaries that can have the following keys:
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 programming language rocks !',
},
{
'title': 'Django website',
'url': 'http://www.djangoproject.com',
'external': True
},
{
'title': 'Some internal link',
'url': '/some/internal/link/',
'external': False
},
)
))
The screenshot of what this code produces:
Module that lists installed apps and their models. As well as the DashboardModule properties, the AppList has two extra properties:
If no include/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',
include_list=('django.contrib',)
))
# will list all apps except the django.contrib ones
self.children.append(modules.AppList(
title='Applications',
exclude_list=('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.
Module that lists a set of models. As well as the DashboardModule properties, the ModelList takes two extra keyword arguments:
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.append(modules.ModelList(
title='Authentication',
include_list=('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.
Module that lists the recent actions for the current user. As well as the DashboardModule properties, the RecentActions takes three extra keyword arguments:
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:
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, the Feed takes two extra keyword arguments:
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: