Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic menu structure with Django?

Tags:

django

menu

I want a menu structure with a menu and a submenu, and depending on what page is currently viewed, I want to highlight items in both menus. Are there any modules or apps that provide this? If not, what would be the best way to approach the problem?

like image 871
henrik Avatar asked Nov 04 '10 08:11

henrik


2 Answers

Quick google search gives this:

http://code.google.com/p/django-treemenus/

http://code.google.com/p/django-menuse/

You can also create such simple menu manually, just pass to the template list of menu items, active menu and list of submenu items for the active menu and the active submenu item:

     <ul>
     {% for item in menu_items %}
         <li>
         {% if item.id == active_menu_item %}
             <span class="active-menu-item">{{ item }}</span>
             <ul>
                   {# Similar code for submenu items #}
             </ul>
         {% else %}
             <a class="inactive-menu-item" href="{{ item.url }}">{{ item }}</a>
         {% endif %}
         </li>
     {% endfor %}
     </ul>
like image 199
Tomasz Zieliński Avatar answered Oct 06 '22 01:10

Tomasz Zieliński


According to djangopackages, the most popular menu application in 2018 is django-sitetree.

Item highlighting can be done with the second tool from the link above, django-activeurl.

For large sites, however, django-sitetree may have performance issues.

There is another way which is related to the hierarchical structure of menus: modified preorder tree traversal, which optimizes database queries for tree lookups.

Use django-mptt to store hierarchical data (such as menus with submenus to unlimited depth). Use django-mptt-admin to easily manage them in admin menu. contenttypes framework may be useful for more generic menus (this is not shown in django-mptt tutorial).

like image 44
Yaroslav Nikitenko Avatar answered Oct 05 '22 23:10

Yaroslav Nikitenko