Breadcrumbs are set up into views, and therefore can only be used with class-based views.
First of all, you’ll probably want to define a base mixin for all your views:
from navutils import BreadcrumbsMixin, Breadcrumb
class BaseMixin(BreadcrumbsMixin):
def get_breadcrumbs(self):
breadcrumbs = super(BaseMixin, self).get_breadcrumbs()
breadcrumbs.append(Breadcrumb('Home', url='/'))
return breadcrumbs
Then, you can inherit from this view everywhere:
# breadcrumbs = Home > Blog
class BlogView(BaseMixin):
title = 'Blog'
# breadcrumbs = Home > Logout
class LogoutView(BaseMixin):
title = 'Logout'
By default, the last element of the breadcrumb is deduced from the title attribute of the view.
However, for a complex hierarchy, you are free to override the get_breadcrumbs method:
# you can trigger url reversing via pattern_name, as for menu nodes
class BlogMixin(BaseMixin)
def get_breadcrumbs(self):
breadcrumbs = super(BlogMixin, self).get_breadcrumbs()
breadcrumbs.append(Breadcrumb('Blog', pattern_name='blog:index'))
return breadcrumbs
# breadcrumbs = Home > Blog > Last entries
class BlogIndex(BlogMixin):
title = 'Last entries'
# for dynamic titles, just override the get_title method
# breadcrumbs = Home > Blog > My category name
class CategoryDetail(BlogMixin, DetailView):
model = Category
def get_title(self):
# assuming your Category model has a title field
return self.object.title
The last step is to render the breadcrumbs in your template. The provided mixin takes
care with passing data in the context, so all you need is:
{% load navutils_tags %}
{% render_breadcrumbs breadcrumbs %}
The breadcrumbs part of navutils is bundled with two templates, feel free to override them:
That’s it !