BSD-licensed menu tools for Django, built by Ross Poulton <http://www.rossp.org>
django-menu provides a basic structure for you to build multiple navigation menus for your website, such as the header menubar. These menus can be easily maintained by staff using the Django administration without any knowledge of HTML or Django internals.
Sub-menus can also be easily built and displayed only for particular URIs.
pip install django-menu
Add
menu
to yourINSTALLED_APPS
./manage.py migrate menu
(or./manage.py syncdb
if you don't use South. You should use South.)Add
django.template.context_processors.request
to yourTEMPLATE
settings. Below is a reasonably safeTEMPLATES
setting for most small projects, however yours may vary.:TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.request', ], }, }, ]
Add a Menu (eg called
headernavigation
) and add some items to that menuIn your template, load the menu tags and embed your primary menu.
<ul>{% load menubuilder %}{% menu headernavigation %} {% for item in menuitems %}<li><a href="{{ item.url }}" title="{{ item.title|escape }}"{% if item.current %} class='current'{% endif %}>{{ item.title }}</a></li> {% endfor %} </ul>
If your template has a spot for navigation for the current sub-level of your
website tree (i.e. a dozen pages underneath /about/
, plus a few under
/products/
) you can create a new menu with a URI
of /about/
.
In your template, instead of the {% menu %}
tag use {% submenu %}
. If a
submenu for the current URI exists, it will be shown. The {{ submenu_items }}
list contains your navigation items, ready to output like in the examples above.
To avoid hitting the database every time a user requests a page, the menu items are
cached if you have a cache configured. Caching is not used when settings.DEBUG
is True
.
To disable caching, set the setting MENU_CACHE_TIME
to -1
or remove your
Django Cache configuration.
To enable caching to continue to let you make items available to anonymous or authenticated users, and to enable the "Current Page" functionality, the cache will contain one dataset for each menu, authentication & path combination.