Beardy Geek – Web Development Blog

A blog about web development – giving tips and tutorials about all aspects of web dev.

Beardy Geek – Web Development Blog

Django quick tips – using template filters in your views

Django has some very useful template filters, like slugify, which turns a string with spaces into a suitable url slug, and title, which turns any string into title case.

Django logo

Have you ever wanted to use these in your views?

All you need to do is add this import:

from django.template.defaultfilters import *

You can replace the asterisk with the name of the filter if you don’t want all of them. Then use the filters as functions (which is what they are anyway.

my_str = title('hello world')
print my_str

>>> Hello World