wagtail bakerydemo有一套很好的菜单,我也想包含主页。
Wagtail希望页面成为家庭的孩子,这是根本的,而菜单则遵循层次结构 -
因此,如果我在navigation_tags中更改top_menu
获取这样的菜单项:
menuitems = (parent.get_siblings()).live().in_menu()
主页显示,但菜单将其视为一个anscestor,而不是平等。
知道如何改变这一点以便“回家”。和它直接的孩子一样吗?
答案 0 :(得分:1)
实现这一目标的一种方法是预先添加一个新的menuitem
作为主页。
假设您只在主菜单中使用了此top_menu
标记,您还可以假设传入标记的parent
始终是site_root
,而menuitems
依次是主页。
唯一的更改是在 @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'],
}
的for循环之后和返回模板上下文之前。
示例:更新了navigation_tags.py
menuitems
注意:{{1}}实际上是queryset not a list,这意味着要向其附加项目,我们需要强制它成为列表。这可能不是执行此操作的最佳方式,您可以调整queryset query以始终包含主页,但这可以完成工作。