Menu app for Django with ordering and ability to link menu item with model instance, view or URL.
Python
Pull request Compare This branch is 1 commit ahead of althalus:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
simplemenu
.gitignore
LICENSE
MANIFEST.in
README.rst
setup.py

README.rst

django-simplemenu

A dead simple menu app for Django with ordering in admin interface and ability to link menu item with model instance, view or URL.

It features user-proof admin interface with links for ordering items and limited list of available pages to link to. You should register your views, QuerySets, model instances or URLs to populate that list.

Installation

  1. Run python setup.py install or just place simplemenu directory into a directory which is on your PYTHONPATH.
  2. Add 'simplemenu' to your INSTALLED_APPS setting.
  3. Run python manage.py syncdb.
  4. Register your pages by adding calls to simplemenu.register.
  5. Add menu to your templates.

Note that this application requires Django 1.1 or newer.

Registering your pages

The idea was to make editing menu via admin interface as simple as possible. So in order to create new menu item user only have to enter its name and choose a page from a drop-down list. You have to register a page to make it available for user.

For example, you could add this code to your urls.py:

import simplemenu

simplemenu.register(
    'blog.views.recent_entries',
    FlatPage.objects.all(),
)

In that case list of pages will be consist of "Recent entries" and names of all existing FlatPages.

You can register the following objects:

  • views (basically anything reversible w/o parameters):

    'app.views.viewname',
    ('app.views.viewname', u'Custom name for the admin interface'),
    
  • QuerySets or model instances (QuerySets will be evaluated every time the menu item form is created. Menu item stores only ContentType and PK of the object. Every object must have get_absolute_url method.):

    Entries.objects.filter(published=True),
    simplemenu.models.URLItem.objects.all(),
    
  • URLs:

    '/some/url/',
    ('/another/url', u'Name of the page'),
    

Templates

This app has only one tag:

{% get_simplemenu as [varname] %}

It stores QuerySet of all menu items in a context variable. Example:

{% load simplemenu_tags %}
{% get_simplemenu as menu %}
{% for item in menu %}
    <a href="{{ item.page.url }}">{{ item.name }}</a>
{% endfor %}

Highlight visited menu items

It's relatively simple to handle menu item that links to current page. First, you need to have URL of the page in your template context. The most common way to do it is to add 'django.core.context_processors.request' to the TEMPLATE_CONTEXT_PROCESSORS setting and to use RequestContext in your views. Then you could write in your template the following:

{% load simplemenu_tags %}
{% get_simplemenu as menu %}
<ul>
{% for item in menu %}
    <li {% ifequal item.page.url request.path %}class="selected"{% endifequal %}>
        <a href="{{ item.page.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>