Git Product home page Git Product logo

django-menu-generator's Introduction

Django Menu Generator

A menu generating application for Django

status-image version-image coverage-image

A productivity tool that enables the generation of full featured menus through python dictionaries list, you only need to setup the HTML structure once for each menu you like to build and then use the dictionaries to generate menu items

Features:

  • Tested support to Python 3.4, 3.5, 3.6, 3.7, 3.8
  • Tested support to Django 1.8, 1.9, 1.10, 1.11, 2.0, 2.2, 3.0, 3.1
  • No database
  • Support unlimited menus
  • Icons support
  • Semi-Automatically identifies the selected item and his breadcrums
  • Conditional menu items display through validators (Permissions, Authentications or whatever you want)

Documentation:

The full documentation for Django Menu Generator is allowed here:

http://django-menu-generator.readthedocs.io/en/latest/index.html

Installation:

You can install it with one of these options:

  • easy_install django-menu-generator
  • pip install django-menu-generator
  • git clone https://github.com/LaLogiaDePython/django-menu-generator
    1. cd django-menu-generator
    2. run python setup.py
  • wget https://github.com/LaLogiaDePython/django-menu-generator/zipball/master
    1. unzip the downloaded file
    2. cd into django-menu-generator-* directory
    3. run python setup.py

Usage:

  1. Once installed, add 'menu_generator' to your INSTALLED_APPS.
  2. Add {% load menu_generator %} to templates that will handle the menus.
  3. Add the list dictionaries containing the menu items to the settings
####################################################################################
Example: settings.py
####################################################################################

NAV_MENU_LEFT = [
    {
        "name": "Home",
        "url": "/",
    },
    {
        "name": "About",
        "url": "/about",
    },
]

NAV_MENU_RIGHT = [
    {
        "name": "Login",
        "url": "login_url_view",  # reversible
        "validators": ["menu_generator.validators.is_anonymous"],
    },
    {
        "name": "Register",
        "url": "register_view_url",  # reversible
        "validators": ["menu_generator.validators.is_anonymous"],
    },
    {
        "name": "Account",
        "url": "/acount",
        "validators": ["menu_generator.validators.is_authenticated"],
        "submenu": [
            {
                "name": "Profile",
                "url": "/account/profile",
            },
            {
                "name": "Account Balance",
                "url": "/account/balance",
                "validators": ["myapp.profiles.is_paid_user"],
            },
            {
                "name": "Account Secrets",
                "url": "/account/secrets",
                "validators": ["menu_generator.validators.is_superuser"],
            }
        ],
    },
]

FOOTER_MENU_LEFT = [
    {
        "name": "Facebook",
        "url": "facebook.com/foobar",
    },
    {
        "name": "Contact US",
        "url": "/contact",
    },
]

FOOTER_MENU_RIGHT = [
    {
        "name": "Address",
        "url": "/address",
    },
]

Or you can build the menu dictionaries list inside the project apps with menus.py files, see docs for more.

  1. In your template, load the template tag to generate your menu.
{% load menu_generator %}
<!DOCTYPE html>
<html>
    <head><title>Django Menu Generator</title></head>
    <body>
        <!-- NAV BAR Start -->
        {% get_menu "NAV_MENU_LEFT" as left_menu %}
        <div style="float:left;">
            {% for item in left_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                <a href="{{ item.url }}"> <i class="{{ item.icon_class }}"></i> {{ item.name }}</a>
                </li>
                {% if item.submenu %}
                    <ul>
                    {% for menu in item.submenu %}
                        <li class="{% if menu.selected %} active {% endif %}">
                            <a href="{{ menu.url }}">{{ menu.name }}</a>
                        </li>
                    {% endfor %}
                    </ul>
                {% endif %}
            {% endfor %}
        </div>

        {% get_menu "NAV_MENU_RIGHT" as right_menu %}
        <div style="float:right;">
            {% for item in right_menu %}
                <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{ item.url }}">{{ item.name }}</a>
                </li>
                {% if item.submenu %}
                    <ul>
                    {% for menu in item.submenu %}
                        <li class="{% if menu.selected %} active {% endif %}">
                            <a href="{{ menu.url }}">{{ menu.name }}</a>
                        </li>
                    {% endfor %}
                    </ul>
                {% endif %}
            {% endfor %}
        </div>
        <!-- NAV BAR End -->

        <!-- Footer Start -->
        {% get_menu "FOOTER_MENU_LEFT" as left_footer_menu %}
        <div style="float:left;">
            <!-- loop through your left footer menus -->
        </div>

        {% get_menu "FOOTER_MENU_RIGHT" as right_footer_menu %}
        <div style="float:right;">
            <!-- loop through your right footer menus -->
        </div>
        <!-- Footer End -->
    </body>
</html>
  1. Now you must to see your menus generated when you run your project

Running the tests:

To run the tests against configured environments:

tox

License:

Released under a (MIT) license.

Author and mantainers:

Milton Lenis - [email protected]

Juan Diego García - [email protected]

Credits:

We would like to thank Val Kneeman, the original author of this project under the name 'menuware' https://github.com/un33k/django-menuware

django-menu-generator's People

Contributors

hansegucker avatar juandgc avatar lachmanfrantisek avatar lucaskuzma avatar miltonln avatar natureshadow avatar yamijuan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-menu-generator's Issues

related_urls match too much

The new related_urls feature matches too much, i.e. in the middle of a string.

Giving /car as related URL will not only match /car/1, but also /drinks/categories/carbonated. Using a trailing / would help a bit, but that would mean that /person is not matched under all circumstances.

Feature Request: menu item decorator

Sometimes it is important to decorate a menu item with a different visual element. It is a way to indicate to the user that something need to be done through the given menu element.

For instance, a badge should be put on a menu item to indicate a necessary action to be taken by the user. But in the current implementation there is no easy way to do so. It is necessary to evaluate the menu item element in order to decorate it properly.

So, during rendering, it should be only a matter of evaluating a single menu item element in order to decide, on a template, how to decorate this menu item.

Allow merging menus with same name

If several apps declare a menu item with the same name, and the item has a submenu, it would be good to merge the two menus together into one. That way, apps cann add menu entries to existing submenues.

It would also be possible to enable or disable merging with an additional attribute.

validators

the validators are not validating with an "and" operator .

If first validator fails, the rest shouldn't be evaluated

Currently I have a menu where I use the is_authenticated validation of menu_generator so I don't to ask that again in my own validator, but due the fact all validators are evaluated, an anonymous user reaches my validator and fails. Here is and example:

MENUS = {
    "NAV_MENU_LEFT": [
        {
            "name": _("Users"),
            "url": "/",
            "icon_class": "fal fa-user",
            "validators": [
                "menu_generator.validators.is_authenticated",
                ("users.validators.has_user_type", "superuser"),
            ],
        }
    ]
}

Feature request: Select menu item if a related URL is opened

If you use submenus in a Django application, they are mostly collapsed if no menu item is selected. Calling a menu item from this submenu will take much longer than if it's already open (marked as selected).

Example with no selected submenu item and collapsed submenus:
grafik

Example with selected submenu item and opened submenu:
grafik

If you are directly on the page with the URL set in the menu config, the correct submenu item is selected and you can easily switch between other submenu items in the same submenu. But often you have a submenu item which is called "Persons" and shows a person list, for example. But what happens if you open a edit or details page for one person? The URL changes, so the submenu item is no longer selected, but you actually want that it's still selected.

Therefore my idea is adding an option to define a list of related URLs for every menu item. If one of these URLs is opened, the menu item will be marked then as selected, too.

Thank you for reading this slightly longer text! I would be happy to get some feedback on this idea and whether an implementation of this idea would be a benefit for this project accepted by you.

Notice: The screenshots are taken from AlekSIS, the free school information system.

Root menu with submenus wrong selected value

The selected value of a root menu is overriden by _process_breadcrums. For example, if the _is_selected function correctly determines that a root menu is selected by accessing a link which contains the root's url, when _process_breadcrums is run, the selected value goes back to False.

As a workaround for this issue you can use:
{% if item.selected or item.url in request.path %} active {% endif %}

Issue with menu in apps when AppConfig is referenced in INSTALLED_APPS

The get_menu_from_apps function is looking for sub classes « menus ». Therefore, when following the recommandations for django 2.0+ which are to put the AppConfig class path in the INSTALLED_APPS instead of the main app class (cf https://docs.djangoproject.com/en/2.0/ref/applications/), the function does not find the MENUS dictionaries.

Ex:
The following works:
INSTALLED_APPS = [
# local apps
‘account',

The following does not work:
INSTALLED_APPS = [
# local apps
‘account.apps.AccountConfig’,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.