django

How to slugify a value using Django's template tags

When generating an attribute for an HTML element, you may want to use a slugified version of a variable. Luckily, Django's built-in templating engine can easily handle this for you.

{{ value|slugify }}

This will turn an input value of My Blog Post into my-blog-post

In another example, lets assume that the value of article.category is Latest Gadgets

<div class="category-{{ article.category|slugify }}">
    {{ article.category }}
</div>

would give the following result:

<div class="category-latest-gadgets">Latest Gadgets</div>

more Django posts