Alex Rivera | Logout

django templates, find string replace with other string

Asked 2010-02-02T15:22:50.473
30

Hay, I'm writing some templates but I want to convert " " into "_" within a string.

I want to convert the output of

{{ user.name }}

from something like "My Name" to "My_Name". How do I do this?

Edit
Report

1 Answer

93

A shorter version of Matthijs' answer:

{{ user.name.split|join:"_" }}

Of course it only works when splitting on whitespace.

answered 2011-07-18T15:23:05.617

Your Answer