1

How can I add support for custom menus which will work also with non-Wagtail based pages.

  1. For example by giving directly a relative url to a registration page such as '/account/registration')
  2. For example by giving directly an absolute url to an external page such as 'www.stackoverflow.com'

I found this very interesting project: https://github.com/rkhleics/wagtailmenus Unfortunately is does not support submenus in the main menu.

Dan Swain
  • 2,910
  • 1
  • 16
  • 36
Philipp S.
  • 827
  • 17
  • 41

1 Answers1

2

One thing about Wagtail is that what I would call the data tree is made up only of pages (it's called a page tree). This tree is used as the basis for navigation but, of course, sometimes you might want a navigation item in this tree to be something other than a page. I accomplish what you want to do by subclassing 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

And in the template:

{% 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 %}
Dan Swain
  • 2,910
  • 1
  • 16
  • 36
  • Thank you for the input. But this is not really what I want. Sometimes I would like to link to an external resource. How would you do this? (I will update my post and add this requirement) – Philipp S. Jul 08 '19 at 15:00
  • 1
    Sure thing. I understand what you're asking, and I've tried wagtailmenus too. I posted this solution because if your project is mostly Wagtail and you're rendering your main menu from the Wagtail page tree, then this provides a way for you to include an occasional link in the Wagtail page tree without having to create any other data structures. You COULD either include the link by hand in the HTML (not suggesting this) or create your own set of model-based links, but if you want to be able to simply render the menu from one data tree (Wagtail's page tree), then this solution will work. – Dan Swain Jul 08 '19 at 15:07
  • 1
    Also see the edit I made to show template rendering logic – Dan Swain Jul 08 '19 at 15:15
  • @PhilippS. Any luck with this problem? – Dan Swain Jul 09 '19 at 12:38
  • Yes, just playing a bit around. But your code helped a lot. I just added this line to not add the "fake page" to the sitemap: def get_sitemap_urls(self): return [] – Philipp S. Jul 09 '19 at 13:20
  • I have one problem left. If I create a Node and leave "self.link" empty (serve will return "pass") then I got an exception if I try to open the "slug" of this page. How can I tell Wagtail internally not to "fire" on that slug? – Philipp S. Jul 10 '19 at 11:00
  • Not sure what you mean by opening the slug. Maybe post as another question and post your code and the error message. – Dan Swain Jul 10 '19 at 12:24
  • Created a new post: https://stackoverflow.com/questions/56972612/how-to-create-an-invisible-dummy-page-in-wagtail – Philipp S. Jul 10 '19 at 14:12