如何向 wagtail 添加多级菜单支持(支持基于非 wagtail 的页面)

How to add multi-level menu support to wagtail (with support for non-wagtail based pages)

如何添加对自定义菜单的支持,该菜单也适用于非基于 Wagtail 的页面。

  1. 例如,直接给注册页面一个亲戚url,例如'/account/registration')
  2. 例如直接给外部页面一个绝对值url,例如'www.whosebug.com'

我发现了这个非常有趣的项目:https://github.com/rkhleics/wagtailmenus 不幸的是不支持主菜单中的子菜单。

关于 Wagtail 的一件事是,我所说的数据树仅由页面组成(称为 page tree)。此树用作导航的基础,但当然,有时您可能希望此树中的导航项不是页面。我通过子类化 Page:

来完成你想做的事情
from django.http import HttpResponseRedirect

class Node(Page):

    subpage_types = [your subpage types]
    parent_page_types = [your parent page types]

    link = models.CharField(max_length=255, default='', blank='True')

    content_panels = Page.content_panels + [
        FieldPanel('link')
    ]    

    def serve(self, request):
        if self.link is not None:
            return HttpResponseRedirect(self.link)
        else:
            pass

并且在模板中:

{% for item in menu_items %}
    <li>
        <a href="{% if item.specific.link and item.specific.link != '' %}{{ item.specific.link }}{% else %}{% pageurl item %}{% endif %}">{{ item.title }
        </a>
    </li>
{% endfor %}