0

我在 django-cms 中有一个自定义应用程序,并且需要将子菜单附加到我的应用程序。我已经按照我找到的指南和示例来执行此操作(请参阅 Brandon 在此处给出的 Portfolio 示例:Djangocms 中的自定义视图?),并设法启动并运行子菜单。

通过扩展上面提供的示例;如果这里展示的这个 Portfolio 应用程序包含少量不同的视图(创建视图、详细视图以及可能的其他几个相关视图)会怎样。如果我需要在这个小应用程序中构建一个子菜单来保存与用户导航相关的选项怎么办?如果导航应该根据视图中的所选内容呈现选择,则何时才能(仅选择“选项”或“类似”)。子菜单必须知道选择了什么投资组合,对吗?或者至少实际上选择并考虑了投资组合。

如何将我的视图已经知道的转移到我的 CMSAttachMenu 实现中?就我而言,我正在实现一个处理聚会或“事件”的应用程序。下面的示例不起作用,因为 Event 对象显然没有在请求中注册,但它说明了我想要的:

# 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)
4

1 回答 1

1

这是一个困难的问题,但以下将解决它(仅显示相关部分):

cms_app.py

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _

class EventsApphook(CMSApp):
    name = _("Event")
    urls = ["App.apps.event.urls"]
apphook_pool.register(EventsApphook)

菜单.py

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

menuNodes = []

class EventMenu(CMSAttachMenu):
    name = _("Event Sub-Menu")
    def get_nodes(self, request):
        return menuNodes
menu_pool.register_menu(EventMenu)

def add_menu_node(text, url):
    # only add a given url once
    if len(list(n for n in menuNodes if n.url == url)) == 0:
        menuNodes.append(NavigationNode(text, url, 1 + len(menuNodes), 0))
        menu_pool.clear()

视图.py

from django.views.generic.detail import DetailView
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from App.apps.event.menu import add_menu_node
from App.apps.event.models import Event

class EventMenuMixin(object):
    def get_context_data(self, **kwargs):
        context = super(EventMenuMixin, self).get_context_data(**kwargs)
        member = self.request.user

        if 'pk' in self.kwargs.keys():
            event = Event.objects.get(id=self.kwargs['pk'])
            if event.is_registered_to_event(member):
                add_menu_node(_('Unregister from this event'), reverse("unregister_from_event"))
            else:
                add_menu_node(_('Register to participate in this event'), reverse("register_to_event", args=(self.kwargs['pk'])))

        add_menu_node(_("Create new events"), reverse("admin:event_event_add"))
        return context

class EventDetailView(EventMenuMixin, DetailView):
    model = Event
    template_name = 'event/event_detail.html'
    context_object_name = 'event'

我希望这能帮助和我遇到同样困境的其他人。

于 2012-12-17T21:45:24.463 回答