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 import Menu
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 import items, Menu
class MyMenu(Menu):
def __init__(self, **kwargs):
super(MyMenu, self).__init__(**kwargs)
self.children.append(
items.MenuItem(title='Home', url=reverse('admin:index'))
)
self.children.append(
items.AppList(title='Applications')
)
self.children.append(
items.MenuItem(
title='Multi level menu item',
children=[
items.MenuItem(title='Child 1', url='/foo/'),
items.MenuItem(title='Child 2', url='/bar/'),
]
),
)
Below is a screenshot of the resulting menu:
Sometimes you may need to access context or request variables to build your menu, 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.
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.items import MenuItem
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:
Helper method that returns True if the menu item is empty. This method always returns False for basic items, but can return True if the item is an AppList.
Helper method that returns True if the menu item is active. A menu item is considered as active if it’s URL or one of its descendants URL is equals to the current URL.
A menu item that lists installed apps an their models. In addition to the parent MenuItem 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 menu item:
from admin_tools.menu import items, Menu
class MyMenu(Menu):
def __init__(self, **kwargs):
super(MyMenu, self).__init__(**kwargs)
self.children.append(items.AppList(
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.
Please refer to the init_with_context() documentation from MenuItem class.
Helper method that returns True if the applist menu item has no children.
>>> from admin_tools.menu.items import MenuItem, AppList
>>> item = AppList(title='My menu item')
>>> item.is_empty()
True
>>> item.children.append(MenuItem(title='foo'))
>>> item.is_empty()
False
>>> item.children = []
>>> item.is_empty()
True
A menu item that lists pages bookmarked by the user. This menu item also adds an extra button to the menu that allows the user to bookmark or un-bookmark the current page.
Here’s a small example of adding a bookmark menu item:
from admin_tools.menu import items, Menu
class MyMenu(Menu):
def __init__(self, **kwargs):
super(MyMenu, self).__init__(**kwargs)
self.children.append(items.Bookmarks(title='My bookmarks'))
Please refer to the init_with_context() documentation from MenuItem class.
A bookmark menu item is never considered as active, the real item is.
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.