I have a TextField with text from a txt file in admin. My txt have linebreaks. The problem is when the TextField are in readonly_fields, all linebreaks dissaper and all content is grouped.
How to keep the format using this field in readonly_fields mode?
The problem does not happen when not in readonly_fields.
Thanks!
I have a TextField with text from a txt file in admin. My txt
Share
A line break in text is generally represented by the characters
\nor\ror often\r\n(check out this article on wikipedia for more info).The problem you’re having is that these characters will be used to display a new line in a text editing field but they don’t represent a new line in html (they’re ignored).
If you want them to display in a read only field then you could replace them with
<br/>elements.If you can mark your string as safe (ie if you can safely add html code without the risk of anyone using the field to add malicious code), then you could override the save method on your model to swap out text line breaks for html line breaks –
Another alternative would be to add full text formatting functionality using a plugin like django-tinymce.
My last suggestion would be to hack at it with javascript. Add an admin folder to your templates and then create a base_site.html file which extends the original and adds a simple javascript function (as described here). Something like –
You’ll need to add a
replacefor every type of new line you have in your text (e.g.\r,\r\n). Whilst this may do what you need, it seems like the dirtiest hack of them all.