Converts a string to a slug, for use in a URL. Specifically, it:
- Converts to lowercase ASCII.
- Converts spaces to hyphens.
- Removes all characters except letters, numbers, underscores, and hyphens.
- Strips leading and trailing whitespace.
Variable
blurb_text = 'Aren’t you a smart one?'
Template
{{ blurb_text|slugify }}
Result
arent-you-a-smart-one
Commentary
The
slugify
filter can be used to coerce an integer in to a string in a template. Consider the following use case:Because
order
is passed on the querystring, it will be a string, butforloop.counter0
will be an integer, so the two will never be equal. Coercingforloop.counter0
to a string usingslugify
solves the problem.An alternative would be to coerce
request.GET.order
in to an integer using theadd
filter as shown here.