import re import translitcodec # needed to install it from django.template import Library from django.template.defaultfilters import stringfilter from django.utils.safestring import mark_safe register = Library() PUNCT_RE = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') @stringfilter def slugify(value, delim=u'-'): """ Generates an ASCII-only slug. Borrowed from http://flask.pocoo.org/snippets/5/ """ result = [] for word in PUNCT_RE.split(value.lower()): word = word.encode('translit/long') if word: result.append(word) return mark_safe(unicode(delim.join(result))) slugify.is_safe = True