[Django]-Insert new child menu items in existing menu

6đź‘Ť

âś…

The answer for this is that I should have been using (and AM using) Attach Menus which are, unfortunately, VERY poorly documented here: https://django-cms.readthedocs.org/en/latest/extending_cms/app_integration.html#attach-menus.

Also, while I was following those instructions, I accidentally imported CMSAttachMenu from menus.base rather than from cms.menu_bases which doesn’t result in any errors, but also doesn’t do anything, so, it was rather difficult to debug =/

Here is some working code in case it helps anyone in the future:

in models.py

from django.db import models

class Sport(models.Model):
  name = models.CharField(max_length=64, blank=True)
  slug = models.SlugField(blank=True)

  def __unicode__(self):
    return self.name

  def get_absolute_url(self):
    return "/sports/" + self.slug

In menu.py

from cms.menu_bases import CMSAttachMenu
from django.utils.translation import ugettext_lazy as _
from menus.base import NavigationNode
from menus.menu_pool import menu_pool

from apps.theproject.models import Sport


class SportSubMenu(CMSAttachMenu):

  name = _("Sports Sub-Menu")

  def get_nodes(self, request):

    nodes = []
    for sport in Sport.objects.order_by('order'):
      node = NavigationNode(
        sport.name,
        sport.get_absolute_url(),
        sport.pk
      )
      nodes.append(node)

    return nodes

menu_pool.register_menu(SportSubMenu)

Once these two files are in-place, restart the service. In Django-CMS, navigate to the page whose menu-item you’d like to have the various Sports object appear as children menu-items in your menu.

In the Advanced Settings section (which is normally collapsed), you’ll see a new option “Attached Menu”, choose the new item “Sports Sub-Menu” and you’ll be in business.

👤mkoistinen

Leave a comment