wagtail - 菜单如何也包含主页?
wagtail - menus how to include homepage as well?
wagtail bakerydemo 有一套不错的菜单,我也想包括主页。
Wagtail 期望页面是 home 的子页面,它位于根目录,而菜单遵循层次结构 -
所以如果我在 navigation_tags
中更改 top_menu
获取这样的菜单项:
menuitems = (parent.get_siblings()).live().in_menu()
主页出现了,但菜单将其视为祖先,而不是平等的。
知道如何更改它以使 'home' 与其直系子项相同吗?
实现此目的的一种方法是在主页前面添加一个新的 menuitem
。
假设您只在主菜单中使用了这个 top_menu
标签,您还可以假设传递到标签中的 parent
将始终是 site_root
,这反过来是主页。
唯一的变化是在menuitems
的for循环之后和模板上下文返回之前。
示例: 已更新 navigation_tags.py
@register.inclusion_tag('tags/top_menu.html', takes_context=True)
def top_menu(context, parent, calling_page=None):
menuitems = parent.get_children().live().in_menu()
for menuitem in menuitems:
menuitem.show_dropdown = has_menu_children(menuitem)
# We don't directly check if calling_page is None since the template
# engine can pass an empty string to calling_page
# if the variable passed as calling_page does not exist.
menuitem.active = (calling_page.url.startswith(menuitem.url)
if calling_page else False)
# assumes menu is only called with parent=site_root and is live + ignores `in_menu` field on homepage
home_page = parent
home_page.show_dropdown = False
home_page.active = (
# must match urls exactly as all URLs will start with homepage URL
(calling_page.url == home_page.url) if calling_page else False
)
# append the home page (site_root) to the start of the menuitems
# menuitems is actually a queryset so we need to force it into a list
menuitems = [home_page] + list(menuitems)
return {
'calling_page': calling_page,
'menuitems': menuitems,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
注意:menuitems
实际上是 queryset not a list, which means to append an item to it we need to force it to become a list. This may not be the most performant way to do this, you could adjust the queryset query 始终包含主页,但这样就完成了工作。
wagtail bakerydemo 有一套不错的菜单,我也想包括主页。
Wagtail 期望页面是 home 的子页面,它位于根目录,而菜单遵循层次结构 -
所以如果我在 navigation_tags
中更改 top_menu获取这样的菜单项:
menuitems = (parent.get_siblings()).live().in_menu()
主页出现了,但菜单将其视为祖先,而不是平等的。
知道如何更改它以使 'home' 与其直系子项相同吗?
实现此目的的一种方法是在主页前面添加一个新的 menuitem
。
假设您只在主菜单中使用了这个 top_menu
标签,您还可以假设传递到标签中的 parent
将始终是 site_root
,这反过来是主页。
唯一的变化是在menuitems
的for循环之后和模板上下文返回之前。
示例: 已更新 navigation_tags.py
@register.inclusion_tag('tags/top_menu.html', takes_context=True)
def top_menu(context, parent, calling_page=None):
menuitems = parent.get_children().live().in_menu()
for menuitem in menuitems:
menuitem.show_dropdown = has_menu_children(menuitem)
# We don't directly check if calling_page is None since the template
# engine can pass an empty string to calling_page
# if the variable passed as calling_page does not exist.
menuitem.active = (calling_page.url.startswith(menuitem.url)
if calling_page else False)
# assumes menu is only called with parent=site_root and is live + ignores `in_menu` field on homepage
home_page = parent
home_page.show_dropdown = False
home_page.active = (
# must match urls exactly as all URLs will start with homepage URL
(calling_page.url == home_page.url) if calling_page else False
)
# append the home page (site_root) to the start of the menuitems
# menuitems is actually a queryset so we need to force it into a list
menuitems = [home_page] + list(menuitems)
return {
'calling_page': calling_page,
'menuitems': menuitems,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
注意:menuitems
实际上是 queryset not a list, which means to append an item to it we need to force it to become a list. This may not be the most performant way to do this, you could adjust the queryset query 始终包含主页,但这样就完成了工作。