如何在 django cms breadcrumbs 中显示完整路径?

How to show in django cms breadcrumbs full path?

需要您的建议 - 如何在面包屑中显示文章或页面的完整路径? 我有代码:

{% for ance in ancestors %}
   <li>
       {% if not forloop.last %}
       <a href="{{ ance.get_absolute_url }}">{{ ance.get_menu_title }}</a>
    {% else %}
      <span class="active">{{ ance.get_menu_title }}</span>
    {% endif %}
   </li>
{% endfor %}

但它只显示 "Home" 和当前页面。如何修改它以在面包屑中显示整个路径?

我在文件 menu_tags.py 中发现了一些可能是调用祖先的代码。至少有一个class ShowBreadcrumbs:

    class ShowBreadcrumb(InclusionTag):
    """
    Shows the breadcrumb from the node that has the same url as the current request

    - start level: after which level should the breadcrumb start? 0=home
    - template: template used to render the breadcrumb
    """
    name = 'show_breadcrumb'
    template = 'menu/dummy.html'

    options = Options(
        Argument('start_level', default=0, required=False),
        Argument('template', default='menu/breadcrumb.html', required=False),
        Argument('only_visible', default=True, required=False),
    )

    def get_context(self, context, start_level, template, only_visible):
        try:
            # If there's an exception (500), default context_processors may not be called.
            request = context['request']
        except KeyError:
            return {'template': 'cms/content.html'}
        if not (isinstance(start_level, int) or start_level.isdigit()):
            only_visible = template
            template = start_level
            start_level = 0
        try:
            only_visible = bool(int(only_visible))
        except:
            only_visible = bool(only_visible)
        ancestors = []
        nodes = menu_pool.get_nodes(request, breadcrumb=True)

        # Find home
        home = None
        root_url = unquote(reverse("pages-root"))
        home = next((node for node in nodes if node.get_absolute_url() == root_url), None)

        # Find selected
        selected = None
        selected = next((node for node in nodes if node.selected), None)

        if selected and selected != home:
            node = selected
            while node:
                if node.visible or not only_visible:
                    ancestors.append(node)
                node = node.parent
        if not ancestors or (ancestors and ancestors[-1] != home) and home:
            ancestors.append(home)
        ancestors.reverse()
        if len(ancestors) >= start_level:
            ancestors = ancestors[start_level:]
        else:
            ancestors = []
        context['ancestors'] = ancestors
        context['template'] = template
        return context


register.tag(ShowBreadcrumb)


def _raw_language_marker(language, lang_code):
    return language


def _native_language_marker(language, lang_code):
    with force_language(lang_code):
        return force_text(ugettext(language))


def _current_language_marker(language, lang_code):
    return force_text(ugettext(language))


def _short_language_marker(language, lang_code):
    return lang_code


MARKERS = {
    'raw': _raw_language_marker,
    'native': _native_language_marker,
    'current': _current_language_marker,
    'short': _short_language_marker,
}

这是我用来实现此目的的完全相同的面包屑模板,所以只要您的页面结构类似于;

Home
  Child
    Grandchild
  Child
    Grandchild

您只需加载带有菜单标签的模板,例如; {% show_breadcrumb 0 "partials/breadcrumb.html" 0 %}

菜单标签上的文档是here

不要忘记,如果您在菜单中隐藏了任何页面,它们也不会显示在面包屑中,因为它们是由使用页面树的菜单标签创建的。

感谢大家的建议,尤其是 makrsweb。我发现原因是页面没有相互链接。我有主页、我的书、Book1、Book2 等作为独立页面,当我链接它们时,使结构看起来像:

Home  
    |My Books 
             |Book1
             |Book2
             |...

(Parent with children and grandchilds) 一切开始正常工作。 问题不是出在代码上,而是出在我的注意力不集中。