from django.template import Library from django.template.defaultfilters import slugify, stringfilter register = Library() @register.filter @stringfilter def slug_and_slash_to_dash(value): """ Converts any slashes in the given value into spaces, then slugify's the result. Leading and Trailing slashes (e.g. /some/url/) are ignored. """ return slugify(' '.join(value.split('/')))