我有一个与Django CMS 3.5.1和Django 1.11.11的网站,它有一个与CMS建立的菜单。它的其中一个页面是一个令人惊讶的事件列表。当我单击事件详细信息时,菜单将失去其活动类。
这是我的菜单:
{% for child in children %}
<li class="js-submenuParent {% if child.selected or child.ancestor %}active{% endif %}">
<a href="{% if child.children %}javascript:void(0);{% else %}{{ child.attr.redirect_url|default:child.get_absolute_url }}{% endif %}" class="{% if child.children %}submenu-btn js-openSubmenu{% endif %}">{{ child.get_menu_title }}</a>
{% if child.children %}
<ul class="submenu js-submenu">
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
我以为child.ancestor
可以解决这个问题(或者甚至是child.selected
,因为我似乎可以使用{% page_attribute "page_title" %}
访问父母的标题),但事实并非如此。我是不是必须创建一个custom apphook menu才能做到这一点,还是有一个更简单的解决方案?
发布于 2018-05-26 05:10:35
在为应用程序集成菜单之前,将应用程序附加到的页面是菜单系统知道的最后一点。
添加菜单允许您的应用程序URL通过URL的NavigationNode(title, url, id)
对象与菜单应用程序集成。
所以如果你有一个细节视图,你可以附加一个简单的菜单;
from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from django.utils.translation import ugettext_lazy as _
from cms.menu_bases import CMSAttachMenu
class EventMenu(CMSAttachMenu):
name = _("event menu")
def get_nodes(self, request):
nodes = []
n = NavigationNode(_('detail page'), "/detail", 1)
nodes.append(n)
return nodes
menu_pool.register_menu(EventMenu)
关于这方面的文档在这里;http://docs.django-cms.org/en/latest/how_to/menus.html#integration-attach-menus
在上面的基础上,你也可以根据列表/查询集等生成节点,就像你通过一些调查发现的那样;
def get_nodes(self, request):
nodes = []
for event in Event.objects.published():
n = NavigationNode(
event.title, # title
event.get_absolute_url(), # url
'id_{slug}'.format(slug=event.slug), # unique ID for node
visible=False
)
nodes.append(n)
return nodes
发布于 2018-05-27 21:09:25
https://stackoverflow.com/questions/50524956
复制相似问题