Django: allow line break from textarea input

HtmlDjango

Html Problem Overview


How do I allow line breaking in textarea input in django to later show this input on page?

Html Solutions


Solution 1 - Html

linebreaks

Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (<br />) and a new line followed by a blank line becomes a paragraph break (</p>).

For example:

{{ value|linebreaks }}

If value is Joel\nis a slug, the output will be <p>Joel<br />is a slug</p>.

Solution 2 - Html

Don't use {% autoescape off %} ! Otherwise user controlled input may not get escaped, which is a security risk. As mentioned use linebreaks or linebreaksbr.

Solution 3 - Html

I had a text area for user minimal custom input in the template and I wanted to keep whatever formatted text the user inserted to stay the same. The solution, I simply changed the Model variable to a TextField type. It even shows the user formatted text in the admin. :)

ex.

class Uadds(models.Model):
	title		= models.CharField(max_length = 50)
	description	= models.TextField(max_length = 1000)

title will not show line breaks, however, description will show them. I haven’t tested this with a Rich Text Editor... Hope this helped.

Solution 4 - Html

Use {% autoescape off %} {{ your_variable }} {% endautoescape %}.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbarinView Question on Stackoverflow
Solution 1 - HtmlbarinView Answer on Stackoverflow
Solution 2 - Htmluser3402383View Answer on Stackoverflow
Solution 3 - HtmlEmanuel FaiscaView Answer on Stackoverflow
Solution 4 - HtmlHaridasView Answer on Stackoverflow