jape
jape

Reputation: 2901

Line breaks Django string

Is there a way to display line breaks in a rendered Django string?

contact_message = "Name: %s | Email: %s" % (
        form_name,
        form_email,
    )

For example, the code above currently prints as:

Name: <rendered name> | Email: <rendered email>

Is there a way to make it print like this:

Name: <rendered name>
Email: <rendered email>

I will be using the send_mail function, so I am hoping to make the readability more visually appealing.

Thank you!

Upvotes: 2

Views: 4140

Answers (3)

Jean-Michel Provencher
Jean-Michel Provencher

Reputation: 103

\n is the way to go in django!

Upvotes: 0

mattions
mattions

Reputation: 312

if you want just to have a new line you can just do it with:

contact_message = "Name: %s \n Email: %s" % (
        form_name,
        form_email,
    )

Upvotes: 1

Klaus D.
Klaus D.

Reputation: 14369

When sending mail, a simple \n should be enough:

contact_message = "Name: %s\nEmail: %s" % (
    form_name,
    form_email,
)

This won't work in HTML, there you need HTML tags and then you have to mark the string as safe: https://docs.djangoproject.com/en/1.8/ref/utils/#module-django.utils.safestring

Upvotes: 3

Related Questions