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

时间:2015-10-10 19:37:07

标签: django django-cms breadcrumbs

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

{% 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 %}

但它仅显示&#34; Home&#34;和当前页面。如何修改它以显示面包屑中的整个路径?

我在文件menu_tags.py中发现了一些可能是调用祖先的代码。至少有一个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,
}

2 个答案:

答案 0 :(得分:1)

这就是我用来实现此目的的完全相同的痕迹导航模板,因此只要您的页面结构类似于;

Home
  Child
    Grandchild
  Child
    Grandchild

您只需要使用菜单标签加载该模板; {% show_breadcrumb 0 "partials/breadcrumb.html" 0 %}

菜单标签上的文档为here

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

答案 1 :(得分:0)

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

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

(有孩子和孙子的父母)一切都开始正常运作。 问题不在于代码,而在于我的疏忽。