[Django]-Why is Django sending the wrong email template?

3đź‘Ť

I cannot recreate anything like what you are describing (similar to Ramiro who answered the ticket, and cannot recreate the problem either). If I have two templates with same base name, different extensions, different contents, and cut and paste from your code to send an email from a view, I get the different contents appearing in the sent mail (on Gmail and checking the “original”).

You say your .html and .txt files are different but it really sounds, based on everything you say, that your .html file has the .txt contents. You’ve got both the file system and app directories loader listed: which one is supposed to be finding these files? Perhaps there is a stray file in a place where the other one is looking that has the wrong contents?

Some experiments in the shell might help you debug. Load the .html template and see whether it really contains what you think it does. For example:

>>> from django.template import loader
>>> from pprint import pprint
>>> template = 'emails/dun'
>>> ht = loader.get_template(template+'.html')
>>> pprint(ht.nodelist)
[<Text Node: '<p><strong>Mr. '>,
 <Variable Node: user>,
 <Text Node: '</strong>: Pay us $ '>,
 <Variable Node: amt>,
 <Text Node: ' before next Friday.</p>
'>]

Verify the .html version is different from the .txt version:

>>> tt = loader.get_template(template+'.txt')
>>> pprint(tt.nodelist)
[<Variable Node: user>,
 <Text Node: ': This is an important me'>,
 <Variable Node: amt>, 
 <Text Node: '.
'>]

If they are the same when loaded, then you need to look into why the right template file isn’t being found or why the one that is found has the wrong contents, and the answer will have nothing to do with the render() code. Only if they are different when loaded and the same upon rendering would looking into render() be necessary.

UPDATE: So based on the question update, the loaded templates are different, in that each extends a differently-named base template. So the next question becomes, are those base templates different? Changing my recreation scenario to match yours, at least so far as the originally-loaded templates extend base templates, I still cannot recreate the problem. I see different templates loaded:

>>> from django.template import loader, Context
>>> from pprint import pprint
>>> template = 'emails/dun'
>>> ht = loader.get_template(template+'.html')
>>> pprint(ht.nodelist)
[<ExtendsNode: extends "emails/base.html">]
>>> tt = loader.get_template(template+'.txt')
>>> pprint(tt.nodelist)
[<ExtendsNode: extends "emails/base.txt">]

That render differently:

>>> c = Context({'user': 'Joe', 'amt': '50.00'})
>>> tt.render(c)
u'\nJoe: This is an important message. You owe us $ 50.00.\n\n'
>>> ht.render(c)
u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xm
lns="http://www.w3.org/1999/xhtml">\n<head>\n<title></title>\n</head>\n<body>\n\n<p><strong>Mr. Joe</strong>: Pay us $ 5
0.00 before next Friday.</p>\n\n</body>\n</html>\n'

Because the base templates referenced have different contents:

>>> template = 'emails/base'
>>> ht = loader.get_template(template+'.html')
>>> pprint(ht.nodelist)
[<Text Node: '<!DOCTYPE html PUBLIC "-/'>,
 <Block Node: title. Contents: []>,
 <Text Node: '</title>
</head>
<body>
'>,
 <Block Node: content. Contents: []>,
 <Text Node: '
</body>
</html>
'>]
>>> tt = loader.get_template(template+'.txt')
>>> pprint(tt.nodelist)
[<Block Node: content. Contents: []>, <Text Node: '
'>]
>>>
👤Karen Tracey

1đź‘Ť

get_template() looks for the first matching template in settings.TEMPLATE_LOADERS

TEMPLATE_LOADERS is usually built from filesystem and app_directories

app_directories checks all the INSTALLED_APPS for templates

So, I’d check all your settings.INSTALLED_APPS for templates that have an ’emails’ folder, and rename your template, or put the one you want first in the INSTALLED_APPS list.

👤Robert

0đź‘Ť

FYI, I think I finally figured out that it’s a bug with the Django Development Server !! I don’t seem to have this problem when I switch it into production and run it on my apache server.

👤mpen

Leave a comment