1

I'm trying to use python's Template object to create html emails. However, when the app tries to send the emails I get the error:

File "/base/data/home/apps/hanksandbox/1.350486862928605153/main.py", line 573, in post
html = messages.email_document_html(document)
File "/base/data/home/apps/hanksandbox/1.350486862928605153/messages.py", line 109, in email_document_html
description = document.get_description()
File "/base/python_runtime/python_dist/lib/python2.5/string.py", line 170, in substitute
return self.pattern.sub(convert, self.template)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 763: ordinal not in range(128)

The code the error refers to looks like:

def email_document_html(document):
email_document = Template("""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    ${stylesheet}
</head>
<html>
<body>
<h1 id="mainhead">Essays</h1>
<div class="document">
<span class="info">At ${date} ${author} published:</span><br/><hr/>
<a href="${domain}/${author}/document/${filename}/" target="_blank"><h1 class="title">${title}</h1></a>
<p class="description">${description}</p>
</div>
</body>
</html>
""")
return email_document.substitute(
                                 stylesheet=style,
                                 date=str(document.date),
                                 author=document.author.username,
                                 domain=domainstring,
                                 filename=document.filename,
                                 title=document.title,
                                 description = document.get_description()
                                 )

My IDE is using UTF-8 as its character set. I've tried adding # coding: utf-8 to my files. I've tried adding .encode("utf-8") or .decode("utf-8") in a variety of places particularly after 'document.get_description()'. But I'm grasping at straws.

Any suggestions?

2 Answers 2

6

Use unicode everywhere.

email_document = Template(u"""
 ...

"Unicode In Python, Completely Demystified"

Sign up to request clarification or add additional context in comments.

Comments

2

Put a u before your template string to specify that it is intended to be a unicode string. Python < 3 interprets strings like that as ASCII unless told otherwise.

email_document = Template(u"""
...

1 Comment

Thanks! I can't believe I melted my brain for hours over one little letter u.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.