Slugify a string

Posted: February 20th, 2010 | Author: Davo | Filed under: Django | Tags: , | No Comments »

The SlugField() converts the string to slug version but what about when we want to convert something to slug? To convert a string into a slug/slugified version, we can use the Django’s based slugify template tag.


>>> from django.template.defaultfilters import slugify
>>> str = 'This Is Just a Random String to Be converted to SLUG strinG!'
>>> slugify(str)
u'this-is-just-a-random-string-to-be-converted-to-slug-string'
>>>

As you can see all the spaces were converted to ‘-’ the ‘!’ was removed.