The error occurs because somewhere code coerces your unicode template string into a python 2 str
; you need to encode the rendered template into an UTF-8 bytestring yourself:
if isinstance(rendered, unicode):
rendered = rendered.encode('UTF-8')
# rendered is now guaranteed to be of type str