Django Flex Menus#

A flexible, developer-friendly Django package for building dynamic, context-aware navigation menus with minimal boilerplate.

Features#

  • Flexible API: Simple Menu and MenuItem classes for building flexible tree structures using the anytree API.

  • Flexible Rendering: Reusable class-based renderers that can be used with any menu structure.

  • Flexible URL resolution: Django view names, static URLs, or callable functions with automatic parameter filtering

  • Flexible Visibility: Show/hide items based on user permissions, authentication, request attributes, database objects.

Installation#

pip install django-flex-menus

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'flex_menu',
]

Quick Start#

1. Define a menu (e.g., in myapp/menus.py):

from flex_menu import Menu, MenuItem

# Create main navigation
main_nav = Menu(
    "main_nav",
    children=[
        MenuItem(name="home", view_name="home"),
        MenuItem(name="about", view_name="about"),
        MenuItem(
            name="products",
            children=[
                MenuItem(name="product_a", view_name="products:detail"),
                MenuItem(name="product_b", view_name="products:detail"),
            ],
        ),
    ],
)

2. Use in templates with URL parameters:

{% render_menu 'main_nav' slug="product-a" %}

3. Configure renderers (in settings.py):

FLEX_MENUS = {
    "renderers": {
        "bootstrap5": "myproject.renderers.Bootstrap5NavbarRenderer",
        "sidebar": "myproject.renderers.Bootstrap5SidebarRenderer",
    },
}

3. Render in template:

{% load flex_menu %}

<!DOCTYPE html>
<html>
<head>
    <title>My Site</title>
</head>
<body>
    {% render_menu 'main_nav' renderer='bootstrap5' %}
    <!-- CSS/JS automatically included before the menu -->
</body>
</html>

Documentation#

License#

MIT License - see LICENSE file for details.