Skip to main content

Django generic view breadcrumbs

Project description

django-view-breadcrumbs

PyPI - Downloads Build Status PyPI - Django Version PyPI - Python Version

Codacy Badge Codacy Badge PyPI version All Contributors

This provides a generic set of breadcrumb mixin classes.

Requires adding {% render_breadcrumbs %} to just the base template.

Screenshot

In the base.html template simply add the render_breadcrumbs tag and any template that inherits the base should have breadcrumbs included. i.e

base.html

{% load view_breadcrumbs %}

{% block breadcrumbs %}
    {% render_breadcrumbs %} {# Optionally provide a template e.g {% render_breadcrumbs "view_breadcrumbs/bootstrap5.html" %} #}
{% endblock %}

And your create.html.

{% extends 'base.html' %}

Breadcrumb mixin classes provided.

  • BaseBreadcrumbMixin - Base view requires a crumbs class property.
  • CreateBreadcrumbMixin - For create views Home / Posts / Add Post
  • DetailBreadcrumbMixin - For detail views Home / Posts / Post 1
  • ListBreadcrumbMixin - For list views Home / Posts
  • UpdateBreadcrumbMixin - For Update views Home / Posts / Post 1 / Update Post 1

Installation:

$ pip install django-view-breadcrumbs

Add app to your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'view_breadcrumbs',
    ...,
]

Settings

Name Default Description Options
BREADCRUMBS_TEMPLATE 'view_breadcrumbs/bootstrap4.html' Template used to render breadcrumbs. Predefined Templates

Usage:

django-view-breadcrumbs includes 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 a crumbs property.

Settings:

To modify the root label site wide use

BREADCRUMBS_HOME_LABEL - Sets the root label (default: Home)

Example

BREADCRUMBS_HOME_LABEL = 'My new home'

Renders

Screenshot

View Configuration:

NOTE: All url config should use a pattern view_name=model_verbose_name_{action}

Actions View Class View name Sample Breadcrumb
list ListView {model.verbose_name}_list Home / Posts
change UpdateView {model.verbose_name}_change Home / Posts / Test - Post / Update Test - Post
detail DetailView {model.verbose_name}_detail Home / Posts / Test - Post

Sample crumbs: Home / Posts / Test - Post

In your urls.py

  urlpatterns = [
      ...
      path('posts/<slug:slug>', views.PostDetail.as_view(), name='post_detail'),
      ...
  ]

views.py

from django.views.generic import DetailView
from view_breadcrumbs import DetailBreadcrumbMixin


class PostDetail(DetailBreadcrumbMixin, DetailView):
    model = Post
    template_name = 'app/post/detail.html'

Sample crumbs: Posts

All crumbs use the home root path / as the base this can be excluded by specifying add_home = False

from django.views.generic import ListView
from view_breadcrumbs import ListBreadcrumbMixin


class PostList(ListBreadcrumbMixin, ListView):
    model = Post
    template_name = 'app/post/list.html'
    add_home = False

Can also override the view breadcrumb by specifying a list of tuples [(Label, view path)].

Custom crumbs: Home / My Test Breadcrumb

URL conf.

urlpatterns = [
   path('my-test-list-view/', views.TestView.as_view(), name='test_list_view'),
   path('my-test-detail-view/<int:pk>/', views.TestView.as_view(), name='test_detail_view'),
]

views.py

from django.urls import reverse
from django.views.generic import ListView
from view_breadcrumbs import ListBreadcrumbMixin
from demo.models import TestModel


class TestView(ListBreadcrumbMixin, ListView):
    model = TestModel
    template_name = 'app/test/test-list.html'
    crumbs = [('My Test Breadcrumb', reverse('test_list_view'))]  # OR reverse_lazy

OR

from django.urls import reverse
from django.views.generic import ListView
from view_breadcrumbs import ListBreadcrumbMixin
from demo.models import TestModel
from django.utils.functional import cached_property


class TestView(ListBreadcrumbMixin, ListView):
    model = TestModel
    template_name = 'app/test/test-list.html'

    @cached_property
    def crumbs(self):
        return [('My Test Breadcrumb', reverse('test_list_view'))]

Overriding the Home label for a specific view

from django.utils.translation import gettext_lazy as _
from view_breadcrumbs import DetailBreadcrumbMixin
from django.views.generic import DetailView
from demo.models import TestModel


class TestDetailView(DetailBreadcrumbMixin, DetailView):
     model = TestModel
     home_label = _('My custom home')
     template_name = 'demo/test-detail.html'

Refer to the demo app for more examples.

Running locally

$ make install-dev
$ make migrate
$ make run

Spins up a django server running the demo app.

Visit http://127.0.0.1:8090

Credits

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Derek

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-view-breadcrumbs-1.0.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

django_view_breadcrumbs-1.0.1-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file django-view-breadcrumbs-1.0.1.tar.gz.

File metadata

  • Download URL: django-view-breadcrumbs-1.0.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

File hashes

Hashes for django-view-breadcrumbs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f9b83dfe40b4deaaf47ca8a876928ab43128fb2c49477fba1cbbd3a4c8ed77f9
MD5 b3da84267e90d8ec00e06ee389549e0d
BLAKE2b-256 cfa53403ea55a1b9a884bc8ca7c3e0fffd09ed719ebfc8185736d57bf1e7a08f

See more details on using hashes here.

File details

Details for the file django_view_breadcrumbs-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_view_breadcrumbs-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

File hashes

Hashes for django_view_breadcrumbs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 91da975f43dd10270a2809cdfcb85d57af6cf30985d2c67564f72b18931ba4a3
MD5 aff312d7a7e2df046266da0f9247d64b
BLAKE2b-256 d2049e0ebd30064c0ec2d855d98cf15f7a71e1ef456b309560007da1041aab40

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page