Giancarlo Zaballero
Giancarlo Zaballero

Reputation: 37

Render django html text in email body format

Below is a field on my forms.py:

body = forms.CharField(label='Paste Email Thread Here',
                       widget=forms.Textarea(attrs={
                                             "class": "body-QAerror",}))

And this is the data that I have entered:

enter image description here

Which I passed onto my html template; however, it didn't retain the email format (with indentions) See below:

From: Firstname, Lastname Sent: Wednesday, February 27, 2019 11:01 AM To: [email protected] Subject: server is unresponsive Importance: High For some unknown reason, the server is not responsive – can’t log in, and affected all end users. I filed a ticket 123456. Please communicate this if some users are asking for the issue. Thanks, Firstname MiddleName 78456431 [email protected]

Is it possible to still retain the email format?

Upvotes: 0

Views: 545

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

HTML ignores whitespace. You'll need to convert those newlines to HTML tags.

There are built-in Django template filters to do this: linebreaks and linebreaksbr.

So you would do for example:

{{ email.body|linebreaks }}

Upvotes: 1

Related Questions