Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace space into &nbsp in Django template?

I have a model with multiline text fields, which was input from a textarea.

Now I want to render the field, but don't want to use <pre> tag.

I just want to replace the new line with <br/>, and I found the linebreakbr filter.

But I also want to replace space with &nbsp;. Seemed not included in the documentation.

Can anyone help?

like image 771
Alfred Huang Avatar asked Sep 18 '14 03:09

Alfred Huang


1 Answers

For replacing a space with &nbsp , you may use one of following Template Tags based solution:

1) nbsp filter Replaces usual spaces in string by non breaking spaces

2) Regular Expression Replace Template Filter This will perform a regular expression search/replace on a string in your template.

3) Stackoverflow post, @Paolo Bergantino's answer Suggests to make a custom template tag

like image 164
Ali Raza Bhayani Avatar answered Oct 12 '22 20:10

Ali Raza Bhayani