Wagtail/Django - 如何找到wagtail root主页的模板名称?

Wagtail/Django - How to find the template name of wagtail root home page?

我有一个使用 Wagtail 开箱即用的默认模型制作的根主页。我目前正在尝试创建一个导航栏 header 以下列方式映射出 children 页面:

{% for menu_page in home_page.get_children.live.in_menu %}

这里的问题是,我使用了错误的标签。上面我用了home_page,但是好像不行。

我可以使用:

{% for menu_page in page.get_children.live.in_menu %}

然后列出 menu_pages,但这对导航栏不起作用,因为它不会总是从根 select children 页面,由于一般 page标签。关于如何在添加之前找到实际模板或页面名称的任何想法:page.get_children.live.in_menu .

至re-iterate,我使用的是开箱即​​用的主页模型:

from wagtail.core.models import Page


class HomePage(Page):
    pass

根页面的标题为首页,类型为首页

感谢您的帮助。

创建模板标记说 nav_tags.py

from django import template

from wagtail.core.models import Page, Site



register = template.Library()


@register.simple_tag(takes_context=True)
def get_site_root(context):
    # This returns a core.Page. The main menu needs to have the site.root_page
    # defined else will return an object attribute error ('str' object has no
    # attribute 'get_children')
    return Site.find_for_request(context['request']).root_page


def has_menu_children(page):
    # This is used by the top_menu property
    # get_children is a Treebeard API thing
    # https://tabo.pe/projects/django-treebeard/docs/4.0.1/api.html
    return page.get_children().live().in_menu().exists()


def has_children(page):
    # Generically allow index pages to list their children
    return page.get_children().live().exists()


def is_active(page, current_page):
    # To give us active state on main navigation
    return (current_page.url_path.startswith(page.url_path) if current_page else False)


# Retrieves the top menu items - the immediate children of the parent page
# The has_menu_children method is necessary because the Foundation menu requires
# a dropdown class to be applied to a parent
@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_path.startswith(menuitem.url_path)
                           if calling_page else False)
    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, calling_page=None):
    menuitems_children = parent.get_children()
    menuitems_children = menuitems_children.live().in_menu()
    for menuitem in menuitems_children:
        menuitem.has_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_path.startswith(menuitem.url_path)
                           if calling_page else False)
        menuitem.children = menuitem.get_children().live().in_menu()
    return {
        'parent': parent,
        'menuitems_children': menuitems_children,
        'request': context['request'],
    }

在模板中可以这样做top_menu.html

{% load navigation_tags wagtailcore_tags %}
{% get_site_root as site_root %}

{% for menuitem in menuitems %}
  <li class="presentation {{ menuitem.title|lower|cut:" " }}{% if menuitem.active %} active{% endif %}{% if menuitem.show_dropdown %} has-submenu{% endif %}">
      {% if menuitem.show_dropdown %}
          <a href="{% pageurl menuitem %}" class="allow-toggle">{{ menuitem.title }} <span><a class="caret-custom dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"></a></span></a>
              {% top_menu_children parent=menuitem %}
              {# Used to display child menu items #}
      {% else %}
          <a href="{% pageurl menuitem %}" role="menuitem">{{ menuitem.title }}</a>
      {% endif %}
  </li>
{% endfor %}

所以 top_menu_children.html

{% load navigation_tags wagtailcore_tags %}

<ul class="dropdown-menu" role="menu">
  {% for child in menuitems_children %}
    <li><a href="{% pageurl child %}" role="menuitem">{{ child.title }}</a></li>
  {% endfor %}
</ul>

你从这里检查wagtailbakerydemo 希望对您有所帮助。