Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: storing new lines in the database from admin and returning them via REST API

I created a web application and I am using the django-generated admin to insert some data. In particular, I have the following field:

description = models.TextField(max_length=10000, null=True, blank=True)

In the admin, it appears as a text area, where I can insert new empty lines. However when I insert something like

Some text followed by a new emtpy line.

Some other text.

the result I obtain in my HTML page invoking the REST API is something like this:

Some text followed by a new emtpy line.   Some other text.

So, some spaces appear in place of the new line. If I drop the empty line and just insert a line break, the empty spaces reduce in numbers.

The REST API accessing that field is returning this:

Some text followed by a new emtpy line.\r\n\r\nSome other text.

How do I preserve the empty lines, given the description is correctly stored into the database?

like image 758
Manu Avatar asked Sep 03 '25 08:09

Manu


1 Answers

The problem can be solved by adding a css directive for the text container:

white-space: pre-line;
like image 172
Manu Avatar answered Sep 04 '25 22:09

Manu