Breadcrumbs for flatpages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)

More like this

  1. Breadcrumbs filter by Oggu 5 years, 2 months ago
  2. URL based breadcrumbs by phlex 6 years ago
  3. Flatpage Suggester Template tag for 404 templates by bradmontgomery 4 years, 2 months ago
  4. Use MEDIA_URL in flatpages by robhudson 6 years ago
  5. Easier and Faster than flatpages. Rendering templates by simpling calling by his path on the URL by mariocesar 2 years, 1 month ago

Comments

bradmontgomery (on June 14, 2009):

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!

#

(Forgotten your password?)