Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:41:19+00:00 2026-06-18T06:41:19+00:00

I have a TextField with text from a txt file in admin. My txt

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:41:21+00:00Added an answer on June 18, 2026 at 6:41 am

    A line break in text is generally represented by the characters \n or \r or 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 –

    from django.utils.safestring import mark_safe
    
    def save(self, *args, **kwargs):
        self.text_field = mark_safe(self.text_field.replace("\n", "<br/>"))
        super(YourModel, self).save(*args, **kwargs)
    

    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 –

    {% extends "admin/base.html" %}
    
    {% block extrahead %}
        <script type="text/javascript">
            window.onload = function () {
                var p_elements = document.getElementById('content-main').getElementsByTagName('p');
                var unixNewLine = new RegExp("\n", "g");
                for (var i = p_elements.length - 1; i >= 0; i--) {
                    p_elements[i].innerHTML = p_elements[i].innerHTML.replace(unixNewLine, '<br/>');
                }
            }
        </script>
    {% endblock %}
    

    You’ll need to add a replace for 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code to download a text file from a website. When the
I have a problem when I post text from a UITeField to PHP, if
I have a JTextFiled and JTable that read data from text file. I want
I have a Textfield, and when i print its text to a NSLog ,
I have made an actionscript that loads an external text file, and scrolls its
i have been using an actionListener to pass text from numerous TextFields into a
I'm learning java and I have a small problem, I'm trying to get text
I have a code that reads a multiple text files from a folder and
I have an issue with writing and reading to text file. I have to
Using .NET I have a text file with comma separated data. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.