Package: django-view-breadcrumbs
django-view-breadcrumbsTable of ContentsBackgroundInstallationAddview_breadcrumbsto your INSTALLED_APPSBreadcrumb mixin classes provided.SettingsCustomizationBREADCRUMBS_TEMPLATESite wideOverriding the breadcrumb template for a single viewBREADCRUMBS_HOME_LABELSite wideOverriding the Home label for a specific viewTranslation supportExampleUsageView Configurationdjango-tables-2URL ConfigurationExamplesSample crumbs:PostsSample crumbs:Home / Posts / Test - PostCustom crumbs:Home / My Test BreadcrumbUsing multiple appsRunning locallyCreditsContributors ✨BackgroundThis package provides a set of breadcrumb mixin classes that can be added to any django class based view and requires adding just{% render_breadcrumbs %}to the base template.In thebase.htmltemplate add therender_breadcrumbstag and any template that inherits the base should have breadcrumbs included.Example:my_app
   |--templates
            |--base.html
            |--create.htmlbase.html{% load view_breadcrumbs %}

{% block breadcrumbs %}
    {% render_breadcrumbs %} {# Optionally provide a custom template e.g {% render_breadcrumbs "view_breadcrumbs/bootstrap5.html" %} #}
{% endblock %}And yourcreate.html.{% extends "base.html" %}Installation$pipinstalldjango-view-breadcrumbsAddview_breadcrumbsto your INSTALLED_APPSINSTALLED_APPS=[...,"view_breadcrumbs",...,]Breadcrumb mixin classes provided.BaseBreadcrumbMixin- Subclasses requires acrumbsclass property.CreateBreadcrumbMixin- For create viewsHome / Posts / Add PostDetailBreadcrumbMixin- For detail viewsHome / Posts / Post 1ListBreadcrumbMixin- For list viewsHome / PostsUpdateBreadcrumbMixin- For Update viewsHome / Posts / Post 1 / Update Post 1DeleteBreadcrumbMixin- For Delete views this has a link to the list view to be used as the success URL.SettingsNOTE :warning:Make sure that"django.template.context_processors.request"is added to your TEMPLATE OPTIONS setting.TEMPLATES=[{"BACKEND":"django.template.backends.django.DjangoTemplates","APP_DIRS":True,"OPTIONS":{"context_processors":["django.template.context_processors.debug","django.template.context_processors.request",# <- This context processor is required"django.contrib.auth.context_processors.auth","django.contrib.messages.context_processors.messages",],},},]Modify the defaults using the following:NameDefaultDescriptionOptionsBREADCRUMBS_TEMPLATE"view_breadcrumbs/bootstrap5.html"Template used to render breadcrumbs.Predefined TemplatesBREADCRUMBS_HOME_LABELHomeDefault label for the root pathCustomizationBREADCRUMBS_TEMPLATESite wideBREADCRUMBS_TEMPLATE="my_app/breadcrumbs.html"Overriding the breadcrumb template for a single viewUpdate thebase.html{% render_breadcrumbs "my_app/breadcrumbs.html" %}BREADCRUMBS_HOME_LABELSite wideBREADCRUMBS_HOME_LABEL="My new home"Overriding the Home label for a specific viewfromdjango.utils.translationimportgettext_lazyas_fromview_breadcrumbsimportDetailBreadcrumbMixinfromdjango.views.genericimportDetailViewfromdemo.modelsimportTestModelclassTestDetailView(DetailBreadcrumbMixin,DetailView):model=TestModelhome_label=_("My new home")template_name="demo/test-detail.html"RendersTranslation supportExampleUsagedjango-view-breadcrumbsincludes generic mixins that can be added to a class based view.Using the generic breadcrumb mixin each breadcrumb will be added to the view dynamically
and can be overridden by providing acrumbsproperty.View ConfigurationNOTE: :warning:Model based views should use a patternview_name=model_verbose_name_{action}ActionsView ClassView nameSample BreadcrumbExamplelistListView{model.verbose_name}_listHome / PostsPosts ExamplecreateCreateView{model.verbose_name}_createHome / Posts / Add PostdetailDetailView{model.verbose_name}_detailHome / Posts / Test - PostchangeUpdateView{model.verbose_name}_updateHome / Posts / Test - Post / Update Test - PostdeleteDeleteView{model.verbose_name}_deleteN/AN/ATemplateViewN/AN/ASee:Custom ViewN/AFormViewN/AN/ASee:Custom ViewN/AAboutViewN/AN/ASee:Custom ViewN/AViewN/AN/ASee:Custom Viewdjango-tables-2ActionsView ClassView nameSample BreadcrumbExampleN/ASingleTableMixinN/AN/ASee:demo table viewN/AMultiTableMixinN/AN/ASee:demo table viewN/ASingleTableViewN/AN/ASame implementation asSingleTableMixinFor more examples see:demo appURL ConfigurationBased on the table of actions listed above there's a strictview_namerequirement that needs to be adhered to in order for breadcrumbs to work.This can be manually entered in yoururls.pyor you can optionally use the following class properties instead of hardcoding theview_name....path("tests/",TestListsView.as_view(),name=TestListsView.list_view_name),path("tests/<slug:slug>/",TestDetailView.as_view(),name=TestDetailView.detail_view_name,),path("tests/<slug:slug>/update/",TestUpdateView.as_view(),name=TestUpdateView.update_view_name,),path("tests/<slug:slug>/delete/",TestDeleteView.as_view(),name=TestDeleteView.delete_view_name,),...ExamplesSample crumbs:PostsIn your urls.pyurlpatterns=[...path("posts/",views.PostList.as_view(),name="post_list"),...# OR...path("posts/",views.PostList.as_view(),name=views.PostList.list_view_name),...]All crumbs use the home root path/as the base this can be excluded by specifyingadd_home = Falsefromdjango.views.genericimportListViewfromview_breadcrumbsimportListBreadcrumbMixinclassPostList(ListBreadcrumbMixin,ListView):model=Posttemplate_name="app/post/list.html"add_home=FalseSample crumbs:Home / Posts / Test - PostIn yoururls.pyurlpatterns=[...path("posts/<slug:slug>/",views.PostDetail.as_view(),name="post_detail"),...# OR...path("posts/<slug:slug>/",views.PostDetail.as_view(),name=views.PostDetail.detail_view_name),...]views.pyfromdjango.views.genericimportDetailViewfromview_breadcrumbsimportDetailBreadcrumbMixinclassPostDetail(DetailBreadcrumbMixin,DetailView):model=Posttemplate_name="app/post/detail.html"breadcrumb_use_pk=FalseCustom crumbs:Home / My Test BreadcrumbURL configuration.urlpatterns=[path("my-custom-view/",views.CustomView.as_view(),name="custom_view"),]views.pyfromdjango.urlsimportreversefromdjango.views.genericimportViewfromview_breadcrumbsimportBaseBreadcrumbMixinfromdemo.modelsimportTestModelclassCustomView(BaseBreadcrumbMixin,View):model=TestModeltemplate_name="app/test/custom.html"crumbs=[("My Test Breadcrumb",reverse("custom_view"))]# OR reverse_lazyORfromdjango.urlsimportreversefromdjango.views.genericimportViewfromview_breadcrumbsimportBaseBreadcrumbMixinfromdemo.modelsimportTestModelfromdjango.utils.functionalimportcached_propertyclassCustomView(BaseBreadcrumbMixin,View):template_name="app/test/custom.html"@cached_propertydefcrumbs(self):return[("My Test Breadcrumb",reverse("custom_view"))]Refer to thedemo appfor more examples.Using multiple appsTo reference models from a different application you need to override theapp_nameclass attribute.Example:
Using aLibrarymodel that is imported from acustomapplication that you want to render in ademoapp view.INSTALLED_APPS=[..."demo","custom",...]demo/views.pyclassLibraryDetailView(DetailBreadcrumbMixin,DetailView):model=Libraryapp_name="demo"...Running locally$gitclonegit@github.com:tj-django/django-view-breadcrumbs.git
$makeinstall-dev
$makemigrate
$makerunSpins up a django server running the demo app.Visithttp://127.0.0.1:8090Creditsdjango-bootstrap-breadcrumbsTo file a bug or submit a patch, please head over todjango-view-breadcrumbs on github.If you feel generous and want to show some extra appreciation:Support me with a :star:Contributors ✨Thanks goes to these wonderful people (emoji key):Derek📖David THENON💻This project follows theall-contributorsspecification. Contributions of any kind welcome!