Stop line breaks in textarea

Can you help me with a problem I'm facing? I have a sentence stored in a textarea in my Postgresql DB and I need to display it on another page. However, if the user's input includes line breaks, like this:

Hello

everybody

The string in my database will contain "\n", but when I try to render it, the line breaks are still visible, making the sentences look like this:

Hello \n everybody

I've tried using CSS properties such as white-space: pre-line; and white-space: pre-wrap;, but they didn't work. I also attempted to replace "\n" with "
" in my Python backend code using:

message.replace("\n","<br/>")

But that didn't solve the issue either. Do you have any suggestions or ideas on how to fix this problem?

Thank you for your assistance.

Answer №1

To preserve line breaks properly in your message, you can use <code>message.replace("\n", "<br/>")
function. Make sure to include the |safe filter within the template:

{{ message|safe }}

This will prevent <br> from being displayed as plain text.

Another option is to utilize the following code snippet:

{{ msg|replace('\n','<br>')|safe }}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

One problem with placing Bootstrap columns inside another column

Currently, I am in the process of working on a website that requires a description section. To achieve this, I decided to use two Bootstrap columns – one with a size of 8 and another with a size of 4, totaling up to 12 grid units. Inside the column sized ...

Linked selection options for state, city, and postal code

I have set up a cascading dropdown list - State -> City -> Pincode . Instead of fetching data from the database, I am using JSON. The dropdown functions smoothly on the local server, but experiences slowness on the web server. Here is a snippet of ...

Is it possible to style a nested ID with CSS?

Imagine a scenario where you are working within an HTML file that you cannot modify, but you have the ability to only edit the CSS through a stylesheet. Is it possible to target an ID within another ID in the same way you can do with classes? #id1 #id2 {s ...

Monitoring changes to a SCSS file

I'm feeling completely baffled. I've put in my best effort to resolve this issue. The problem lies with my SCSS file and its watcher that doesn't appear to be functioning properly. A regular CSS file is not being generated at all. Here&apos ...

Step by step guide for adding a hyperlink to a <div> element in iOS

It has come to my attention that the following code snippet is not functioning correctly on iOS devices: <a href = "somelink"> <div class = "samplecalssname"> </div> </a> Shouldn't there be a link on the div for it to ...

How can you make the coordinates of the cursor track the cursor when it hovers over a rectangle?

The code provided below will constantly display the cursor's coordinates right below the cursor: function displayCursorCoordinates(e) { var x = event.clientX; var y = event.clientY; var coordinates = "(" + x + ", " + y + ")"; document.getEl ...

Adjusting the size of h1 when hovering over an image

Can anyone help me figure out how to increase the width of my h1 tag when hovering over the image? I've been struggling to make it work. http://jsfiddle.net/xJ4dc/ Thank you in advance for any assistance. ...

JavaScript for validating dimension text input has unique functionalities

I am looking for guidance on validating an ASP textbox using JavaScript. My goal is to validate a user's input in the format of 4-1/2, 80-1/2, 6/8, or simply 5. Only these types of patterns should be allowed, and each input must contain only one numbe ...

Issues with CSS rendering font in a suboptimal way

Whenever I set a simple font rule for my text, the output is not displayed correctly and appears blurry. The font looks fine in Mozilla Aurora browser but displays poorly in newer versions of Google Chrome and Firefox. Does anyone know why this happens? I ...

What is the best way to expand my container element and add margins to the top and bottom?

Currently, I am utilizing the Material-UI library in combination with ReactJS, but facing some challenges pertaining to flexbox. The objective is to position my Item Container at the center of my div, extending it to occupy the entire available height whi ...

An error message stating 'instructions.addEventListener is not a function' appears when using PointerLockControls in three.js

I've been trying to implement PointerLockControls into my project using the example from the THREEJS examples page. I copied the code exactly as it is, but I keep getting errors in the console and the program won't run. My project structure is pr ...

Select the top row of a table when the checkbox is ticked to emphasize it

Previously, I tackled a challenge on a webpage using jQuery where checkboxes in a table needed to be selected based on specific data attributes. Essentially, if one checkbox in a row was selected, the rest of the checkboxes would be disabled if their data ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

What is the best way to transform an HTML table into the Bootstrap grid system without losing any spacing?

To ensure compliance with the Americans with Disabilities Act, I needed to use an unordered list in my cart instead of a table. Unfortunately, this change has caused some issues with the CSS styles that were previously applied. Currently, I am attempting ...

Is it considered good practice to utilize Vue.js for managing global shared state and implementing for loops outside of components?

Just getting started with Vue.JS and experimenting with creating two lists of books (read and unread) from a shared object in the global data state. This is my approach - working demo (works like a charm! However, I'm wondering if this is considered ...

The dynamic addition of the active class is malfunctioning

I have dynamically added a class using AngularJS, but it seems to not be functioning correctly. As I am new to AngularJS, I would appreciate any suggestions to resolve this issue. Below is my HTML code: <div class="col-md-15 col-sm-3" ng-repeat="data i ...

Using v-model with an input file is not supported

Is there a solution for not being able to use v-model in an input tag with type="file"? Here is an example of the HTML code causing this issue: <input v-model="imageReference" type="file" name="file"/> ...

applying a blur effect to the Vue modal mask

Trying to incorporate a blur panel behind my modal using vue.js, however, The issue arises when the modal-mask contains nested content. This makes it challenging to apply the filter: blur() property without blurring everything. Currently, I am only able t ...

Ways to modify the standard font size for displayed markdown text

I am utilizing a textarea that initially captures text with SimpleMDE (markdown) and is then stored in mysql. When retrieving the data, I utilize the 'Marked' markdown renderer to display the contents within a div element as shown below: <div ...

Retrieve the input value without a specified ID in HTML

After searching extensively, I have not been able to find a solution to my issue. I have a table that displays data from a database and in jQuery, I have successfully implemented a functionality where a row can be added with empty inputs and then submitted ...