简体   繁体   English

换行Django字符串

[英]Line breaks Django string

Is there a way to display line breaks in a rendered Django string? 有没有办法在渲染的Django字符串中显示换行符?

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. 我将使用send_mail函数,所以我希望使可读性更具视觉吸引力。

Thank you! 谢谢!

When sending mail, a simple \\n should be enough: 发送邮件时,简单\\n就足够了:

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 这不适用于HTML,你需要HTML标签,然后你必须将字符串标记为安全: 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的方式!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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