简体   繁体   中英

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!

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

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,
    )

\\ n是进入django的方式!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM