Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django render_to_string missing information

Tags:

django

Why my email (in the body part) does not contain the message?

Here is my code:

message = render_to_string('contact_template.txt', {'contact_name':   contact_name, 'contact_email': contact_email, 'form_content': content}, context_instance=RequestContext(request))
email = EmailMessage("New contact form submission", message, "[email protected]" +'', ['[email protected]'], headers = {'Reply-To': contact_email })
email.send()

My template (contact_template.txt), (all my received emails contained this, but no message) :

Contact Name:


Email:


Content:

and my views:(I have to say it is intimidating...)

from polls.forms import ContactForm
from django.core.mail import EmailMessage
from django.template import Context, Template, RequestContext
from django.shortcuts import render
from django.shortcuts import redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string, get_template

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

def contact(request):
    form_class = ContactForm

    # logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name', '')
            contact_email = request.POST.get('contact_email', '')
            content = request.POST.get('content', '')         
        
            message = render_to_string('contact_template.txt', {'contact_name': contact_name, 'contact_email': contact_email, 'form_content': content}, context_instance=RequestContext(request))
        
            email = EmailMessage("New contact form submission", message, "[email protected]" +'', ['[email protected]'], headers = {'Reply-To': contact_email })
            email.send()
            return redirect('contact')

    return render(request, 'contact.html', {'form': form_class,})
like image 701
Anna Drybulska Avatar asked Apr 30 '16 18:04

Anna Drybulska


1 Answers

For clarity purposes, I would try to limit yourself to a maximum numbers of characters per line. This makes reading the render_to_string line very hard, and makes it even harder to find errors.

from django.template.loader import render_to_string

context = {
    'contact_name': contact_name, 
    'contact_email': contact_email, 
    'form_content': content
}
message = render_to_string('contact_template.txt', context, 
                           context_instance=RequestContext(request))

It seems you are missing locations in the template where the variables are to be printed. You define the following variables:

  • 'contact_name'
  • 'contact_email'
  • 'form_content'

They are however not used in the template. Example:

Contact Name:
{{ contact_name }}

Email:
{{ contact_email }}

Content:
{{ form_content }}
like image 88
Lucas Moeskops Avatar answered Oct 15 '22 15:10

Lucas Moeskops