Why is Django sending the wrong email template?

I can not find the error in this code. I tried to isolate the problem, but it works when I copy the corresponding code to a separate file. The problem should be related to the surrounding code, but I do not see how relevant this is. Everybody is here:

The problem is with the message "Activate your PROJECT account". He sends me an email with something like this:

--===============1413769924==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

text here

--===============1413769924==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

text here

Where the "text here" is the same for both versions of the email, but it is not in the actual files, not when I try to send emails from another script. Here is the code:

def join_transporter(request):
    form = TransporterJoinForm(request.POST)

    if request.POST and form.is_valid():
        user = User(
            username = form.username.val,
            first_name = form.first_name.val,
            last_name = form.last_name.val,
            email = form.email.val,
            is_active = False,
        )
        user.set_password(form.password1.val)
        user.save()

        Profile.objects.create(
            user = user,
            phone = form.phone.val,
            company_name = form.company_name.val,
            details = Transporter.objects.create(),
            address = Address.objects.create(
                city = form.address.city.val,
                province = form.address.province.val,
                country = form.address.country.val,
                street = form.address.street.val,
                postal_code = form.address.postal.val
            )
        )

        e = send_multipart_email('Activate Your PROJECT Account', 
            'emails/transporter_joined', 
            {'user':user, 'fee': settings.PROJECT_FEE * Decimal('100.00'),'settings':settings},
            [user.email],
            send_immediately=False
        )
        e.attach_file('/home/PROJECT/webapps/media/files/InsuranceLetter.pdf')
        e.send() # <------- PROBLEM IS HERE

        send_multipart_email('Transporter Joined', 
            'emails/staff_transporter_joined', 
            {'trans':user,'settings':settings},
            ['service@PROJECT.com','EMAIL@gmail.com']
        )
        messages.info(request, 'Thank you for registering. Please check your email for details on how to activate your account.')
        return redirect('home')





def send_multipart_email(subject, template, data_dict, recipient_list, from_email=settings.DEFAULT_FROM_EMAIL, send_immediately=True):
    if not isinstance(recipient_list, list): recipient_list = [recipient_list]

    d = {'settings':settings} # default context vars
    d.update(data_dict)
    c = Context(d)

    try:
        tt = loader.get_template(template+'.txt')
    except:
        try:
            ht = loader.get_template(template+'.html')
        except:
            raise Exception('Neither `%(tmpl)s.txt` or `%(tmpl)s.html` could be found.' % {'tmpl':template})
        else:
            e = EmailMultiAlternatives(subject, ht.render(c), from_email, recipient_list)
            e.content_subtype = 'html'
    else:
        e = EmailMultiAlternatives(subject, tt.render(c), from_email, recipient_list)

        try:
            ht = loader.get_template(template+'.html')
        except:
            pass
        else:
            e.attach_alternative(ht.render(c), 'text/html')

    if send_immediately:
        e.send()

    return e




[PROJECT@SERVER emails]$ ls
account_activated.txt      shipper_accepted_bid.txt         transporter_awarded_shipment.txt
base.html                  shipper_joined.html              transporter_bid_declined.html
base.txt                   shipper_joined.txt               transporter_bid_declined.txt
forgot_password.html       staff_transporter_joined.html    transporter_joined.html
forgot_password.txt        staff_transporter_joined.txt     transporter_joined.txt
invoice_generated.html     transporter_approved.html        transporter_lost_auction.html
invoice_generated.txt      transporter_auction_closed.html  transporter_lost_auction.txt
shipper_accepted_bid.html  transporter_auction_closed.txt

Ticket: http://code.djangoproject.com/ticket/13364

+3
source share
3

, ( , , ). , , , , , ( Gmail "",).

, .html .txt , , , , .html .txt-. , : ? , , , , ?

. .html , , . :

>>> 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>
'>]

, .html .txt:

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

, , , , , (). , render().

UPDATE: , . , , ? , , , . :

>>> 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">]

:

>>> 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'

, , :

>>> 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: '
'>]
>>>
+3

get_template() settings.TEMPLATE_LOADERS

TEMPLATE_LOADERS filesystem app_directories

app_directories INSTALLED_APPS

, settings.INSTALLED_APPS , "emails", , , INSTALLED_APPS.

+1

FYI, I think I finally realized that this is a bug with the Django development server! I don't seem to have this problem when I include it in production and run it on my Apache server.

0
source

Source: https://habr.com/ru/post/1741514/


All Articles