7. 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.

7.1. Create the navigation menu

We create the menu using a CMSAttachMenu sub-class, and use the get_nodes() method to add the nodes.

For this we need a file called cms_menus.py in our application. Add cms_menus.py in polls_cms_integration/:

  1. from django.urls import reverse
  2. from django.utils.translation import gettext_lazy as _
  3. from cms.menu_bases import CMSAttachMenu
  4. from menus.base import NavigationNode
  5. from menus.menu_pool import menu_pool
  6. from polls.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. node = NavigationNode(
  16. title=poll.question,
  17. url=reverse('polls:detail', args=(poll.pk,)),
  18. id=poll.pk, # unique id for this node within the menu
  19. )
  20. nodes.append(node)
  21. return nodes
  22. menu_pool.register_menu(PollsMenu)

What’s happening here:

  • we define a PollsMenu class, and register it

  • we give the class a name attribute (will be displayed in admin)

  • 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 won’t actually do anything until attached to a page. In the Advanced settings of the page to which you attached the apphook earlier, select “Polls Menu” from the list of Attached menu options, and save once more. (You could add the menu to any page, but it makes most sense to add it to this page.)

select the 'Polls Menu'

You can force the menu to be added automatically to the page by the apphook if you consider this appropriate. See Adding menus to apphooks for information on how to do that.

Note

The point here is to illustrate the basic principles. In this actual case, note that:

  • If you’re going to use sub-pages, you’ll need to improve the menu styling to make it work a bit better.

  • Since the Polls page lists all the Polls in it anyway, this isn’t really the most practical addition to the menu.