Skip to content

Instantly share code, notes, and snippets.

@stober
Created January 18, 2013 19:46
Show Gist options
  • Save stober/4567812 to your computer and use it in GitHub Desktop.
Save stober/4567812 to your computer and use it in GitHub Desktop.
A useful method to turn titles into slugs for urls. BSD code from deep in the heart of Django.
import re
import unicodedata
def slugify(value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and
trailing whitespace.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '-', value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment