Запустите меню страницы не с root_page, а со второго уровня
У меня есть дерево страниц, которое начинается с 3 страниц:
/private
/business
/education
на каждой из этих страниц есть несколько других страниц внутри.
Например, как трясогузка, я создал шаблон тега, чтобы реализовать верхнюю навигацию в моем базовом шаблоне. Особенность в том, что мое меню разделено на два раздела: первый уровень (частный, бизнес, образование) отображается в верхней части моего веб-сайта (меню разделов), а дочерние элементы этих страниц лежат в моей панели начальной загрузки (главное меню). как я могу сказать модели top_menu игнорировать первый уровень, потому что она уже показана в меню моего раздела?
menu_tags.py
from django import template
register = template.Library()
@register.assignment_tag(takes_context=True)
def get_site_root(context):
return context['request'].site.root_page
def has_menu_children(page):
return page.get_children().live().in_menu().exists()
@register.inclusion_tag('tags/section_menu.html', takes_context=True)
def section_menu(context, parent, calling_page=None):
menuitems_section = parent.get_children().filter(
live=True,
show_in_menus=True
)
return {
'calling_page': calling_page,
'menuitems': menuitems_section,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
@register.inclusion_tag('tags/top_menu.html', takes_context=True)
def top_menu(context, parent, calling_page=None):
menuitems = parent.get_children().filter(
live=True,
show_in_menus=True
)
for menuitem in menuitems:
menuitem.show_dropdown = has_menu_children(menuitem)
return {
'calling_page': calling_page,
'menuitems': menuitems,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
# Retrieves the children of the top menu items for the drop downs
@register.inclusion_tag('tags/top_menu_children.html', takes_context=True)
def top_menu_children(context, parent):
menuitems_children = parent.get_children()
menuitems_children = menuitems_children.live().in_menu()
return {
'parent': parent,
'menuitems_children': menuitems_children,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
section_menu.html
{% load menu_tags wagtailcore_tags %}
{% for menuitem in menuitems %}
<li class="{% if calling_page.url == menuitem.url %} active{% endif %}">
<a href="{% pageurl menuitem %}">{{ menuitem.title }}</a>
</li>
{% endfor %}
top_menu.html
{% load menu_tags wagtailcore_tags %}
{% get_site_root as site_root %}
{% for menuitem in menuitems %}
<li class="{% if menuitem.show_dropdown %}dropdown{% endif %}{% if menuitem.active %} active{% endif %}">
{% if menuitem.show_dropdown %}
<a data-toggle="dropdown" class="dropdown-toggle" href="#">{{ menuitem.title }} <b class="caret"></b></a>
{% top_menu_children parent=menuitem %}
{% else %}
<a href="{% pageurl menuitem %}">{{ menuitem.title }}</a>
{% endif %}
</li>
{% endfor %}
header.html (где лежит меню моего раздела)
<header class="ms-header ms-header-dark">
<div class="departement-switcher">
<div class="container">
<ul>
{% block menu %}
{% get_site_root as site_root %}
{% section_menu parent=site_root calling_page=self %}
{% endblock %}
</ul>
</div>
</div>
</header>
navbar.html (где находится мое меню по умолчанию)
{% load menu_tags static %}
<nav class="navbar navbar-static-top yamm ms-navbar ms-navbar-dark">
<div class="container container-full">
<div class="navbar-header">
<a class="navbar-brand" href="/">
<img src="{% static 'img/logo.svg' %}" alt="Data Quest AG">
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
{% block menu %}
{% get_first_level as site_root %}
{% top_menu parent=site_root calling_page=self %}
{% endblock %}
</ul>
</div>
<a href="javascript:void(0)" class="sb-toggle-left btn-navbar-menu">
<i class="zmdi zmdi-menu"></i>
</a>
</div>
</nav>