How to implement breadcrumbs in a Django template?

前端 未结 12 2059
無奈伤痛
無奈伤痛 2020-12-07 09:45

Some solutions provided on doing a Google search for \"Django breadcrumbs\" include using templates and block.super, basically just extending the base blocks and adding the

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 10:29

    You could also reduce the boiler plate required to manage breadcrumbs using django-view-breadcrumbs, by adding a crumbs property to the view.

    urls.py

    urlpatterns = [
        ...
        path('posts/', 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'
    

    base.html

    {% load django_bootstrap_breadcrumbs %}
    
    {% block breadcrumbs %}
        {% render_breadcrumbs %}
    {% endblock %}
    

提交回复
热议问题