-1

Need your advice - how to show in breadcrumbs whole path to article or page? I've got code:

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

but it shows only "Home" and current page. How to modify it to show whole path in breadcrumbs?

I've found in file menu_tags.py something that could be a code that calls ancestors. At least there is a 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,
}
St Pavel
  • 339
  • 1
  • 3
  • 18

2 Answers2

1

That is the exact same breadcrumb template that I use to achieve this, so as long as your page structure is similar to;

Home
  Child
    Grandchild
  Child
    Grandchild

You just need to load that template with the menu tags like; {% show_breadcrumb 0 "partials/breadcrumb.html" 0 %}

The docs on the menu tags are here

Don't forget, that if you have any pages hidden from the menus, they won't display in the breadcrumbs either because they're created by the menu tags using the page tree.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • Mine code is with that zeroes, but still don't loads :( – St Pavel Oct 11 '15 at 19:20
  • @StPavel you don't have any pages with the menu option turned off do you? – markwalker_ Oct 11 '15 at 19:24
  • Are you talking abut pages which are hidden from navigation menu? If so, then yes, I've got such pages. Almost all pages are hidden form navigation menu. – St Pavel Oct 11 '15 at 19:35
  • You'll probably find that they are the pages not showing in your breadcrumbs then. The breadcrumbs are a menu so if you hide them from the menu, you can't use the menu tags to display them. – markwalker_ Oct 11 '15 at 20:21
0

Thanks all for advices, especially makrsweb. I found out that the reason was that pages weren't linked to each other. I've got Home, My books, Book1, Book2 etc. as standalone pages, when I linked them, making structure looking like:

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

(Parent with childs and grandchilds) everything started to works normally. The problem was not in the code, but in my inattentiveness.

St Pavel
  • 339
  • 1
  • 3
  • 18