In my project I've added a newsletter feed. But when trying to send emails with this function :
def send(request):
template_html = 'static/newsletter.html'
template_text = 'static/newsletter.txt'
newsletters = Newsletter.objects.filter(sent=False)
subject = _(u"Newsletter")
adr = NewsletterEmails.objects.all()
for a in adr:
for n in newsletters:
to = a.email
from_email = settings.DEFAULT_FROM_EMAIL
subject = _(u"Newsletter Fandrive")
text = get_template(template_text)
html = get_template(template_html)
d = { 'n': n,'email': to }
text_content = text.render(d)
html_content = html.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
using those templates :
//text
=================== Newsletter - {{ n.date }} ============
==========================================================
{{ n.title }}
==========================================================
{{ n.text }}
==========================================================
//html
<html>
<head>
</head>
<body>
<div style="">
<div style="">
<h1 style="">{{ n.title }} - {{n.date}}</h1>
<p style="">
{{ n.text }}
</p>
</div>
</div>
</body>
</html>
and models :
class Newsletter(models.Model):
title = models.CharField("title", blank=False, max_length=50)
text = models.TextField("text", blank=False)
sent = models.BooleanField("sent", default=False)
data = models.DateTimeField("creation date", auto_now_add=True, blank=False)
class NewsletterEmails(models.Model):
email = models.EmailField(_(u"e-mail address"),)
I'm getting :
TemplateSyntaxError at /utils/newsletter_send/
Caught an exception while rendering: 'dict' object has no attribute 'autoescape'
in {{ n.date }} within text_email template
Although my debug shows I'm sending proper newsletter objects to the template ,as well as debug context :
context {'email': u'[email protected]', 'n': <Newsletter: Newsletter object>}
Why is that happening ? From what I've found about this error it is somehow connected to sending empty dictionary to template renderer, but mine's not empty...