Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Snippet for Django breadcrumbs
# Settings.py
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'base.context_processors.breadcrumbs',
],
home = (_('Start'), 'home')
breadcrumbs_views = {'home': [home],
'index': [(_('Landing'), 'index')],
contact’: [home, (_(‘Contact’), ‘contact’')],
}
# context_procesors.py
from django.core.urlresolvers import resolve
from django.conf.settings import breadcrumbs_views
def breadcrumbs(request):
"""
Context processor for include general breadcrumb in the mains views
:return: dictionary with the breadcrumb for each view
"""
view_name = resolve(request.path_info).url_name
if view_name in breadcrumbs_views:
return {'breadcrumbs': breadcrumbs_views[view_name]}
return {}
# Base.html
{% block breadcrumbs %}
{% include 'breadcrumbs.html'%}
{% endblock breadcrumbs %}
# Breadcrumbs.html
<div class="breadcrumbs">
{% for crumb in breadcrumbs %}
{% if not forloop.last %}
<a class="breadcrumb "href="{% url crumb.1 %}"> {{ crumb.0 }} </a>
>
{% else %}
<a class="breadcrumb current">{{ crumb.0 }}</a>
{% endif %}
{% endfor %}
</div>
@Rewzaw
Copy link

Rewzaw commented Aug 11, 2020

why you're not rendering breadcrumbs.html in breadcrumbs view?

@iPablo
Copy link
Author

iPablo commented Aug 11, 2020

This gist has 4 years, maybe my code is older for new django versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment