This section describe the API of the django-admin-tools menu and menu items. Make sure you read this before creating your custom menu.
This is the base class for creating custom navigation menus. A menu can have the following properties:
If you want to customize the look of your menu and it’s menu items, you can declare css stylesheets and/or javascript files to include when rendering the menu, for example:
from admin_tools.menu.models import *
class MyMenu(Menu):
class Media:
css = ('/media/css/mymenu.css',)
js = ('/media/js/mymenu.js',)
Here’s a concrete example of a custom menu:
from django.core.urlresolvers import reverse
from admin_tools.menu.models import *
class MyMenu(Menu):
def __init__(self, **kwargs):
super(MyMenu, self).__init__(**kwargs)
self.children.append(
MenuItem(title='Home', url=reverse('admin:index'))
)
self.children.append(
AppListMenuItem(title='Applications')
)
self.children.append(
MenuItem(
title='Multi level menu item',
children=[
MenuItem('Child 1'),
MenuItem('Child 2'),
]
),
)
Below is a screenshot of the resulting menu:
This is the base class for custom menu items. A menu item can have the following properties:
Like for menus, menu items 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 items, for example, let’s build a “history” menu item, that will list the last ten visited pages:
from admin_tools.menu.models import *
class HistoryMenuItem(MenuItem):
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(MenuItem(
title=item['title'],
url=item['url']
))
# 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:
A menu item that lists installed apps an their models. In addition to the parent MenuItem properties, the AppListMenuItem 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 menu item:
from admin_tools.menu.models import *
class MyMenu(Menu):
def __init__(self, **kwargs):
super(MyMenu, self).__init__(**kwargs)
self.children.append(AppListMenuItem(
title='Applications',
exclude_list=('django.contrib',)
)
The screenshot of what this code produces:
Note
Note that this module takes into account user permissions, as a consequence, if a user has no rights to change or add a Group for example, the django.contrib.auth.Group model child item won’t be displayed in the menu item.
Customization of the django-admin-tools modules
The django-admin-tools dashboard and dashboard modules API
Enter search terms or a module, class or function name.