I'm having trouble with Django templates and CharField models.
So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug Somename_Somesurname and gets displayed as expected on the template.
However, if I create an object, Somename Somesurname (notice the second space), slug Somename__Somesurname is created, and although on the Django console I see this as <Object: Somename Somesurname>
, on the template it is displayed as Somename Somesurname.
So do Django templates somehow strip spaces? Is there a filter I can use to get the name with its spaces?
Best Answer
Let me preface this by saying @DNS's answer is correct as to why the spaces are not showing.
With that in mind, this template filter will replace any spaces in the string with
Usage:
Output would be
hey there world
Here is the code:
For notes on how template filters work and how to install them, check out the docs.