Django render_to_string

Submitted 3 years, 8 months ago
Ticket #111
Views 728
Language/Framework Python
Priority Urgent
Status Closed

So my issue is that when i try to render_to_string and send the email with that message it shows only on email the HTML code as you can see on pictures

ps: the email receives well the name already fixed the only issue is the template not rendering

It should be something like this

With strip_tags

Those light grey strips is where it should be some variables like name, etc
I used {{client.name}} for example

Submitted on Aug 25, 20
add a comment

1 Answer

Verified

@Brunoseixas  Can you try the below code,

from django.utils.html import strip_tags

message = strip_tags(message)

And then send mail using this message variable. 

Hereafter, send me your code when you submit the ticket along with your screenshot.

Submitted 3 years, 8 months ago

Now it render the html but not with the variables from database it showed like you can see above

- BrunoSeixas 3 years, 8 months ago

if you like to add variable then add the context ={} dictionary.
Example render_to_string('base.html', {'context': 'values'})

- Vengat 3 years, 8 months ago

Okay but how can i send users into context because i could send like client.name and marketingup but when i try to

{% for upsell in marketingup %} {% if upsell.user == user%} it doenst render anything and leaves a blank space in there

- BrunoSeixas 3 years, 8 months ago

May be try this,
def index(request): if not request: context = {'param1':'value1'} return render_to_string('myapp/index.html', RequestContext(None, context)) else: #render as usual render(request, 'myapp/index.html', context) pass

- Vengat 3 years, 8 months ago

solved had to send a request.user so it could work well tnx :D

- BrunoSeixas 3 years, 8 months ago

by the way is there a way to make text bigger or smaller?? where i want on the html to appear on the email

- BrunoSeixas 3 years, 8 months ago

you can customize your html in order to render the text.

- Vengat 3 years, 8 months ago

i had with bootstrap but didnt went well xD i was using <hr> and it didnt showed them on the email

and i have like <h1> and <h4> and there is no difference

- BrunoSeixas 3 years, 8 months ago

add html_message in your send_mail and try it out,
send_mail( 'email title', msg_plain, 'some@sender.com', ['some@receiver.com'], html_message=msg_html, )

- Vengat 3 years, 8 months ago

what is the msg_html?? is it render_to_string('message.html', context) ?

- BrunoSeixas 3 years, 8 months ago

I mean its your message html_message = message

- Vengat 3 years, 8 months ago

but then shows this __init__() got an unexpected keyword argument 'html_message'

- BrunoSeixas 3 years, 8 months ago

Oh you are not using send_mail function. Let me check

- Vengat 3 years, 8 months ago

How about this one, mail = EmailMultiAlternatives(subject, message, from_email, recipient_list) if message: mail.attach_alternative(message, 'text/html') return mail.send() if doesnt work then try change  from EmailMultiAlternatives to send_mail

- Vengat 3 years, 8 months ago

Can you confirm about this one?. Is this resolved or not? If not resolved let me at the earliest , we can investigate further.

- Vengat 3 years, 8 months ago

In future, if the issue resolved, close the ticket from your end at the earliest.

- Vengat 3 years, 7 months ago


Latest Blogs