Extending the navigation menu

You may have noticed that while our Polls application has been integrated into the CMS, with plugins, toolbar menu items and so on, the site’s navigation menu is still only determined by django CMS Pages.

We can hook into the django CMS menu system to add our own nodes to that navigation menu.

For this we need a file called menu.py in the Polls application:

  1. from django.core.urlresolvers import reverse
  2. from django.utils.translation import ugettext_lazy as _
  3. from cms.menu_bases import CMSAttachMenu
  4. from menus.base import Menu, NavigationNode
  5. from menus.menu_pool import menu_pool
  6. from .models import Poll
  7. class PollsMenu(CMSAttachMenu):
  8. name = _("Polls Menu") # give the menu a name this is required.
  9. def get_nodes(self, request):
  10. """
  11. This method is used to build the menu tree.
  12. """
  13. nodes = []
  14. for poll in Poll.objects.all():
  15. # the menu tree consists of NavigationNode instances
  16. # Each NavigationNode takes a label as its first argument, a URL as
  17. # its second argument and a (for this tree) unique id as its third
  18. # argument.
  19. node = NavigationNode(
  20. poll.question,
  21. reverse('polls:detail', args=(poll.pk,)),
  22. poll.pk
  23. )
  24. nodes.append(node)
  25. return nodes
  26. menu_pool.register_menu(PollsMenu) # register the menu.

What’s happening here:

  • we define a PollsMenu class, and register it
  • we give the class a name attribute
  • in its get_nodes() method, we build and return a list of nodes, where:
  • first we get all the Poll objects
  • … and then create a NavigationNode object from each one
  • … and return a list of these NavigationNodes

This menu class is not active until attached to the apphook we created earlier. So open your cms_app.py and add:

  1. menus = [PollsMenu]

to the PollsApp class.

Now, any page that is attached to the Polls application have, below its own node in the navigation menu, a node for each of the Polls in the database.