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
slugifyfilter can be used to coerce an integer in to a string in a template. Consider the following use case:<select name="order"> {% for field in order_fields %} <option value="{{ forloop.counter0 }}" {% if request.GET.order == forloop.counter0|slugify %}selected{% endif %} >{{ field }}</option> {% endfor %} </select>Because
orderis passed on the querystring, it will be a string, butforloop.counter0will be an integer, so the two will never be equal. Coercingforloop.counter0to a string usingslugifysolves the problem.An alternative would be to coerce
request.GET.orderin to an integer using theaddfilter as shown here.