Django ignoring every escape sequence in a template


I’m trying to display ‘desc’ value from dictionary below in a template

4: {‘name’: ‘charmander’, ‘desc’: ‘Obviously prefers\nhot places. When\nit rains, steam\x0cis said to spout\nfrom the tip of\nits tail.’}

However Django is ignoring every escape sequence and it turns out like this on the page, with words glued to each other:
“The plant blooms when it is absorbing solarenergy. It stays on the move to seek sunlight.”

Is there a possibility to turn on some filter which will ignore only “\n” escape sequences?

I’ve tried autoescape and safe tag, however I know these are for ignoring escape sequences not including them in the display

Have you looked at the “page source” in your browser to verify that those characters were stripped out? My initial thought is that they weren’t - but HTML ignores them. HTML does not use the linefeed character as a newline of text in a displayed page.

Thanks for the response!
I’ve just checked that and it seems that “\n” are stripped out, when “\x0c” are not when inspecting the page.
That looks good then, but how do I make HTML read “\x0c”?

Right now they appear like this when inspecting the page

Screenshot from 2021-02-04 18-05-40

What are you expecting to have happen with that character? It looks like it’s rendering as it should for those cases where you’re using a font that doesn’t have a displayable character at that code point.

If you’re looking to print text on the page in a manner that you’re used to with, say, a console application, be aware of the differences between HTML being rendered and text being displayed in a terminal. “Control characters” that you may be used to using in a terminal generally don’t mean anything in HTML.

So if I apply some font in CSS, it could fix it and appear as a space between these words?

“The plant blooms when it is absorbing solarenergy . It stays on the move to seek sunlight.”

would turn into

“The plant blooms when it is absorbing solar energy . It stays on the move to seek sunlight.”

If you had a font that generated a space for that hex value, yes. (And unless you wanted all your text to show up in that font, you’re still going to need to wrap that one character in a span tag.)

Personally, I think overall, you’re going to be a lot better off if you just filter the text in your view before rendering it.

You haven’t identified where this text is coming from - it might be helpful to know what you’re dealing with regarding the source of this data.

It’s coming from external API. At the time of writing this it was displayed directly after parsing it from the API, but now I managed to parse the data to my database and display it that way

Is it better to filter it during parsing data to my database or before sending it to the template? I’m using generic CBV ListView

I definitely would filter it before saving it to the database - do it once, and never have to worry about doing it again. That’s a lot better than trying to do it every time you’re rendering it in a page.

1 Like

Thank you very much for your help, once again! :slight_smile: if you have any links on how to strip these type of tags I would highly appreciate that

I would probably use the Python regex library, sub method to replace any of the to-be-replaced characters with a space.

didn’t come up with that, simple yet powerful. thank you once again!