50
{'quotes': u'Live before you die.\n\n"Dream as if you\'ll live forever, live as if you\'ll die today"\n\n"Love one person, take care of them until you die. You know, raise kids. Have a good life. Be a good friend. Try to be completely who you are, figure out what you personally love and go after it with everything you\'ve got no matter how much it takes." -Angelina Jolie.'}

Notice my dictionary has line breaks in them: \n

How do I display my template with those line breaks?

{{quotes|withlinebreaks\n}}

5 Answers 5

111

Use the linebreaks filter.

For example:

{{ value|linebreaks }}

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

42

You can also use the linebreaksbr filter to simply convert all newlines to <br> without additional <p>.

Example:

{{ value|linebreaksbr }}

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

The difference from Ignacio's answer (linebreaks filter) is that linebreaks tries to guess the paragraphs in a text and wrap every paragraph in <p> where linebreaksbr simply substitutes newlines with <br>.

Here's a demo:

>>> from django.template.defaultfilters import linebreaks
>>> from django.template.defaultfilters import linebreaksbr
>>> text = 'One\nbreak\n\nTwo breaks\n\n\nThree breaks'
>>> linebreaks(text)
'<p>One<br />break</p>\n\n<p>Two breaks</p>\n\n<p>Three breaks</p>'
>>> linebreaksbr(text)
'One<br />break<br /><br />Two breaks<br /><br /><br />Three breaks'
3
  • 2
    This is better I don't know why people vote for wrong Answers. Commented Aug 14, 2021 at 6:36
  • 1
    This solution helped preserve my CSS settings. The top solution using {{value|linebreaks}} ignored them.
    – Anthony M
    Commented Sep 18, 2022 at 1:55
  • "This is better I don't know why people vote for wrong Answers." How dare they do that :-D But I agree. I prefer having the br tags as linebreaks since p seems to do weird things with text in particular html containers.
    – Harlin
    Commented Jan 31 at 15:06
2

All of these answers don't explicitly mention that {{ value|linebreaksbr }} should be used in the display template ie the get request not the post request.

So if you had a template to display say post.details, it would be

<h4>Below are the post details</h4>

{{ post.details|linebreaksbr }}

And not in the post form

<form method="post">
    {% csrf_token %}
    {{ form|linebreaksbr }}
    <button>Save post</button>
</form>

Had this same issue and after figuring it our decided that someone outthere might find it handy. Happy coding!

0

for some reason i can't change this value

val = """
Brand AER \nDetail Material Kuningan berlapis emas chrome dan powder coating \nTipe Produk Keran Shower \nWarna Emas \nMaterial Kuningan \nUkuran Kemasan 12cm x 19cm x 13cm \nUkuran Barang 12cm x 19cm x 13cm \nBerat 2kg \nManual Instruksi instalasi manual dapat didownload di link berikut:https://aer.co.id/products/aer-kran-shower-panas-dingin-luxury-series-sah-sr1/?attachment_id=10954&amp;download_file=5e267c93cdc76&amp;_wpnonce=28db09bc61
"""

which has '\n' in it with linebreaks or linebreaksbr filter i've tried this

{ val|linebreaks }}
{ val|linebreaksbr }}

the val stays the same then i tried @AndrewFox answer and did this and it worked for me

Change \n to <br> and autoescape off

{% autoescape off %}
   {{ val }}
{% endautoescape %}
-5

Use '<br>' instead of /n.

If necessary, you can do a .replace('/n', '<br>').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.