Skip to main content

Menu helpers for django projects

Project description

# django-menus #

django-menus is an app that provides some useful template helpers for rendering and handling menus within django projects.

To use in in your django project, it needs to be installed:

$ pip install -E /path/to/your/venv django-menus

And `"menus"` needs to be in your `settings.INSTALLED_APPS`.

## menu_item

An inclusion template tag that will create a single instance of a menu item, which will only be rendered if the logged in user can access the referenced view. Secondly, the currently active view will have a CSS class of `active` in it's menu item.

{% load menu_item %}

{% menu_item "/foo/" "Foo" %}
{% menu_item "/bar/" "Bar" %}

If we were viewing `/foo/`, this renders to:

<a class="active" href="/foo/">Foo</a>
<a href="/bar/">Bar</a>

Using the standard template. If you want, you can override the `menus/item.html` template to change the display format.

You may also pass in a string like `"url:foo_name"` to the first argument. This will do a `reverse('foo_name')` call (with no args or kwargs) to find a matching url.

If the menu item title is `'home'` (case insensitive), or the url path is `'/'`, then an exact match will be required to mark it as active, otherwise a prefix match is done. This means that if you had a menu item as above, and were viewing the url `/foo/bar/`, then the first menu_item would be marked as active.


## tree_menu

An extension to [django-mptt](https://github.com/django-mptt/django-mptt/), this is a template that you can use to have a dynamic tree menu, where selecting items with children expands them, and selecting a leaf node follows the link. To use it, you'll need to have mptt installed into your project as well as this package.

You use it like:

{% load mptt_tags %}

{% block tree_menu %}
{% full_tree_for_model app_label.ModelName as menu %}
{% include "menu/tree-menu.html" %}
{% endblock %}

If you want it to dynamically hide/show nested data, then you will want to have:

<script src="{{ STATIC_URL }}menus/js/tree-menu.js"></script>
<link rel="stylesheet" href="{{ STATIC_URL }}menus/style/tree-menu.css"
type="text/css" media="screen" title="no title" charset="utf-8">

Somewhere in your page.

This part is currently in use in one small part of a project, and may change if I start to use it more. This README is a little light on because I haven't touched this code in a long, long time.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-menus-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file django-menus-1.0.1.tar.gz.

File metadata

File hashes

Hashes for django-menus-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9c25fcbe24a7ebfd2c4a63fe000b358a2645a0f50b4b0a58c5e1a409be1fc9e7
MD5 70aa52815469da8a1c18558bbdae8e81
BLAKE2b-256 5a4774bdb8c23f7627f28f721f00a37496d54c3c540a3084987cd540015ec1ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page