I have a custom app inside django-cms and have the need to attach a submenu to my app.
I’ve followed the guides and examples I’ve found to do this (see the Portfolio example given by Brandon here: custom views within Djangocms?), and managed to get the submenu up and running.
By expanding on the example provided above; What if this Portfolio app presented here, consisted of a small number of different views (create view, detail view and perhaps a couple of other related views). What if I needed to build a submenu to hold the choices related to user navigation in this small app. And what if the navigation should present choices based on selected content in the views (“Edit” only if a portfolio is selected or similar).
The submenu would have to know what Portfolio was selected, right? Or at least that a Portfolio is in fact selected and in view.
How can I transfer to my implementation of CMSAttachMenu what my view allready knows? In my case, I’m implementing an app dealing with meetups or “Events”. The example below does not work, because the Event object is obviously not registered in the request, but it illustrates what I want:
# menu.py
from django.core.urlresolvers import reverse
from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from cms.menu_bases import CMSAttachMenu
from App.apps.event.models import Event
from django.utils.translation import ugettext_lazy as _
import logging
logger = logging.getLogger('instant.event')
class EventMenu(CMSAttachMenu):
name = _("Event Sub-Menu")
def get_nodes(self, request):
nodes = []
nodes.append(NavigationNode(_('Create new events'), reverse("admin:event_event_add"), 1 + len(nodes), 0))
if hasattr(request, 'event'):
if request.event.is_registered_to_event(request.user):
nodes.append(NavigationNode(_('Unregister from this event'), reverse("unregister_from_event"), 1 + len(nodes), 0))
else:
nodes.append(NavigationNode(_('Register to participate in this event'), reverse("unregister_from_event"), 1 + len(nodes), 0))
if request.user.is_superuser():
nodes.append(NavigationNode(_('Register other participant to this event'), reverse("register_admin", args=(request.event.id)), 1 + len(nodes), 0))
nodes.append(NavigationNode(_('Back to list of events'), reverse("events"), 1 + len(nodes), 0))
return nodes
menu_pool.register_menu(EventMenu)
This was a hard one, but the following would solve it (showing only relevant parts):
cms_app.py
menu.py
views.py
I hope this will help others in the same predicament as me.