from django import template
from django.contrib.flatpages.models import FlatPage
register = template.Library()
def crumbs(url, title):
"Return breadcrumb trail leading to URL for this page"
t = title
s = ' > '
c = '<a href="/">Home</a>'
l = url.split('/')
for index, item in enumerate(l):
if item == '':
del l[index]
n = len(l)
if n > 1:
l[0] = '/' + l[0] + '/'
for i in range(1, n-1):
l[i] = l[i-1] + l[i] + '/'
for index2, item2 in enumerate(l):
q = FlatPage.objects.filter(url=l[index2])
if q:
qa = '<a href="%s">%s</a>' % (q[0].url, q[0].title)
c = c + s + qa
c = c + s + t
return c
register.filter('crumbs', crumbs)
Comments
Great Snippet that works perfectly. Just a heads-up for anyone else wanting to use this: Don't forget that the app in which this template is installed must be included in INSTALLED_APPS in settings.py.
For example, I have a utils app that contains code used project-wide. I installed this snippet into myproject.utils.templatetags.make_breadcrumb_trail but the {% load make_breadcrumb_trail %} failed until I added 'myproject.utils' to my INSTALLED_APPS.
Thanks!
#