Skip to main content

Django generic view breadcrumbs

Project description

django-view-breadcrumbs

Test Codacy Badge pre-commit.ci status Codacy Badge PyPI version Updates

PyPI - Django Version PyPI - Python Version Downloads

All Contributors

Table of Contents

Background

This 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.

breadcrumbs

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

Example:

my_app
   |--templates
            |--base.html
            |--create.html

base.html

{% load view_breadcrumbs %}

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

And your create.html.

{% extends "base.html" %}

Installation

$ pip install django-view-breadcrumbs

Add view_breadcrumbs to your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    "view_breadcrumbs",
    ...,
]

Breadcrumb mixin classes provided.

  • BaseBreadcrumbMixin - Subclasses 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
  • DeleteBreadcrumbMixin - For Delete views this has a link to the list view to be used as the success URL.

Settings

NOTE :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:

Name Default Description Options
BREADCRUMBS_TEMPLATE "view_breadcrumbs/bootstrap5.html" Template used to render breadcrumbs. Predefined Templates
BREADCRUMBS_HOME_LABEL Home Default label for the root path

Customization

BREADCRUMBS_TEMPLATE

Site wide
BREADCRUMBS_TEMPLATE = "my_app/breadcrumbs.html"
Overriding the breadcrumb template for a single view

Update the base.html

{% render_breadcrumbs "my_app/breadcrumbs.html" %}

BREADCRUMBS_HOME_LABEL

Site wide
BREADCRUMBS_HOME_LABEL = "My new home"
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 new home")
     template_name = "demo/test-detail.html"

Renders

custom-root-breadcrumb

Translation support

Example

translated-crumbs

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.

View Configuration

NOTE: :warning:

  • Model based views should use a pattern view_name=model_verbose_name_{action}
Actions View Class View name Sample Breadcrumb Example
list ListView {model.verbose_name}_list Home / Posts
create CreateView {model.verbose_name}_create Home / Posts / Add Post
detail DetailView {model.verbose_name}_detail Home / Posts / Test - Post
change UpdateView {model.verbose_name}_update Home / Posts / Test - Post / Update Test - Post
delete DeleteView {model.verbose_name}_delete N/A
N/A TemplateView N/A N/A See: Custom View
N/A FormView N/A N/A See: Custom View
N/A AboutView N/A N/A See: Custom View
N/A View N/A N/A See: Custom View

django-tables-2

Actions View Class View name Sample Breadcrumb Example
N/A SingleTableMixin N/A N/A See: demo table view
N/A MultiTableMixin N/A N/A See: demo table view
N/A SingleTableView N/A N/A Same implementation as SingleTableMixin

For more examples see: demo app

URL Configuration

Based on the table of actions listed above there's a strict view_name requirement that needs to be adhered to in order for breadcrumbs to work.

This can be manually entered in your urls.py or you can optionally use the following class properties instead of hardcoding the view_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,
    ),
...

Examples

Sample crumbs: Posts

In your urls.py

  urlpatterns = [
      ...
      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 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

Sample crumbs: Home / Posts / Test - Post

In your urls.py

  urlpatterns = [
      ...
      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.py

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


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

Custom crumbs: Home / My Test Breadcrumb

URL configuration.

    urlpatterns = [
       path("my-custom-view/", views.CustomView.as_view(), name="custom_view"),
    ]

views.py

from django.urls import reverse
from django.views.generic import View
from view_breadcrumbs import BaseBreadcrumbMixin
from demo.models import TestModel


class CustomView(BaseBreadcrumbMixin, View):
    model = TestModel
    template_name = "app/test/custom.html"
    crumbs = [("My Test Breadcrumb", reverse("custom_view"))]  # OR reverse_lazy

OR

from django.urls import reverse
from django.views.generic import View
from view_breadcrumbs import BaseBreadcrumbMixin
from demo.models import TestModel
from django.utils.functional import cached_property


class CustomView(BaseBreadcrumbMixin, View):
    template_name = "app/test/custom.html"

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

Refer to the demo app for more examples.

Using multiple apps

To reference models from a different application you need to override the app_name class attribute.

Example: Using a Library model that is imported from a custom application that you want to render in a demo app view.

INSTALLED_APPS =  [
    ...
    "demo",
    "custom",
    ...
]

demo/views.py

class LibraryDetailView(DetailBreadcrumbMixin, DetailView):
    model = Library
    app_name = "demo"
    ...

Running locally

$ git clone git@github.com:tj-django/django-view-breadcrumbs.git
$ make install-dev
$ make migrate
$ make run

Spins up a django server running the demo app.

Visit http://127.0.0.1:8090

Credits

To file a bug or submit a patch, please head over to django-view-breadcrumbs on github.

If you feel generous and want to show some extra appreciation:

Support me with a :star:

Buy me a coffee

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Derek

📖

David THENON

💻

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-2.3.0.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

django_view_breadcrumbs-2.3.0-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-view-breadcrumbs-2.3.0.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.3 pkginfo/1.8.3 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.6.15

File hashes

Hashes for django-view-breadcrumbs-2.3.0.tar.gz
Algorithm Hash digest
SHA256 17dbeb718c1cae601d198fcd83a167d1af1d5398f203298c4ce80ecc61665310
MD5 02c1a3f508d7e85322d8867f6e0f73f4
BLAKE2b-256 5d9aacb1f6debdf755972e2a8d87aa49e53d1977bfc96294d78dd18d321edee0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_view_breadcrumbs-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.3 pkginfo/1.8.3 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.6.15

File hashes

Hashes for django_view_breadcrumbs-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a711173fc0189783e984e5297a23d76a741fbba30d24d8ba2e696092e568183
MD5 9ddc7cd7cbf5e0c1feb35a84231aedd9
BLAKE2b-256 4dd1088c9850a0c3e387b2e0a48a81d52ad58d88d995a1122380ae67da8e9210

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