thperret / django-navutils

A lightweight package for handling menus and breadcrumbs in your django project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django-navutils

Note: this package is still in beta. It has been successfully used in a few projects of my own. However, API may be subject to backward incompatible changes until the first major version is released.

Django-navutils is a lightweight package for handling menu and breadcrumbs inside your django project.

Features

  • No database involved (unless you want it): menus and breadcrumbs are plain old python code
  • Highly customizable
  • Conditionnal menu items display: you want to show a menu link to authenticated users only ? Anonymous ? Staff members ? A custom criteria ? You're covered !
  • i18n-friendly: you can rely on usual django translation mechanisms
  • Unlimited menus
  • Semi-automatic current menu node detection

Requirements

  • Python >= 2.7 or >= 3.3
  • Django >= 1.7

The menu system may be integrated in any project, but the breadcrumbs part requires that you use class-based views.

Install

Package is available on pip and can be installed via pip install django-navutils.

You'll also have to add navutils to your settings.INSTALLED_APPS

Also add the following to settings.CONTEXT_PROCESSORS:

Usage

Menus

Navutils represents menus using Menu and Node instances, each menu being a collection of node instances representing a menu link. Nodes may have children, which are also Node instances.

Let's see a minimal example.

yourapp/menu.py:

yourapp/templates/index.html:

{% load navutils_tags %}
{% render_menu menu=menus.main user=request.user %}

For an anonymous user, this would output something like:

You can also directly set children nodes on parent instanciation with the children argument:

Nodes can be customized in many ways:

Current node

You'll probably want to highlight the current node in some way. Navutils provide a view mixin you an inherit from in order to achieve this.

Assuming the following menu:

You can bind a view to a menu node with the following code:

Under the hood, the mixin will pass the value to the context and a current class will be added

to the login node if the view is displayed. Note that you can achieve the same result with django function-based views, as long as you manually pass the node identifier in the context, under the current_menu_item key.

Node reference

Navutils provide a few node subclasses that address common use cases.

Node

The base Node type, will be displayed to anybody.

AnonymousNode

Displayed to anonymous users only.

AuthenticatedNode

Displayd to authenticated users only.

StaffNode

Displayed to staff users/superusers only.

PermissionNode

Displayed to users that have the given permission. Usage:

AllPermissionsNode

Displayed to users that match a list of permission. Usage:

AnyPermissionsNode

Displayed to users that match any given permission. Usage:

PassTestNode

Displayed to users that match a custom test. Usage:

If it's not enough, you can also override the default templates:

  • navutils/menu.html : the menu wrapper that loop through the nodes
  • navutils/node.html : called for displaying each node instance

And of course, you're free to create your own sub-classes.

Breadcrumbs

Breadcrumbs are set up into views, and therefore can only be used with class-based views.

First of all, you'll probably want to define a base mixin for all your views:

Then, you can inherit from this view everywhere:

By default, the last element of the breadcrumb is deduced from the title attribute of the view. However, for a complex hierarchy, you are free to override the get_breadcrumbs method:

The last step is to render the breadcrumbs in your template. The provided mixin takes care with passing data in the context, so all you need is:

{% load navutils_tags %}

{% render_breadcrumbs breadcrumbs %}

The breadcrumbs part of navutils is bundled with two templates, feel free to override them:

  • navutils/breadcrumbs.html: the breadcrumbs wrapper
  • navutils/crumb.html: used to render each crumb

That's it !

Changelog

See CHANGES.rst.

License

Project is licensed under BSD license.

About

A lightweight package for handling menus and breadcrumbs in your django project

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Python 95.3%Language:HTML 4.7%