[Fixed]-Extending the page in Django

1πŸ‘

βœ…

To show the page extension in the menus (for example, to enable page icons shown in the menu), you first need to add the icon information to the navigation node. In the snippet below, I am adding ability to fetch pagemenuiconextension, which is defined very similarly to your page extension. The menu extension needs to be placed in cms_menus.py:

from menus.base import Modifier
from menus.menu_pool import menu_pool
from raven.contrib.django.raven_compat.models import client

from cms.models import Page

class MenuModifier(Modifier):
    """
    Injects page object into menus to be able to access page icons
    """
    def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
        # if the menu is not yet cut, don't do anything
        if post_cut:
            return nodes

        for node in nodes:

            try: 
                if "is_page" in node.attr and node.attr["is_page"]:

                    class LazyPage(object):
                        id = node.id
                        page = None
                        def pagemenuiconextension(self):
                            try:
                                if not self.page:
                                    self.page = Page.objects.get(id=self.id)
                                return self.page.pagemenuiconextension
                            except AttributeError, ae:
                                # print ae
                                return False
                            except Exception, e:
                                print e
                                client.captureException()

                    node.pageobj = LazyPage()

                else:
                    pass
            except Exception, e:
                client.captureException()

        return nodes

menu_pool.register_modifier(MenuModifier)

Then you can access it in the menu file (snippet from the corresponding menu item):

<div class="{{ child.pageobj.pagemenuiconextension.menu_navicon }}" style="height: 16px;">{{ child.get_menu_title }}</div>

The page extension looks something like this:

class PageMenuIconExtension(PageExtension):

    class Meta:
        verbose_name = _("menu and robots settings")
        verbose_name_plural = _("menu and robots settings")

    menu_navicon = models.CharField(
        _("Page icon"),
        choices=MENU_ICON_CHOICES,
        blank=True,
        max_length=50        
    )

In my case I am using a dropdown to select a class, not an image itself but the principle is the same if you used any other data.

πŸ‘€petr

Leave a comment