Django Jinja2 - AttributeError: 'Environment' object has no attribute 'get_source'
I have the following two lines of code. Both are using jinja2 to replace the template variables:
subject = Environment(loader=BaseLoader).from_string(template.email_subject).render(context)
body = Environment(loader=BaseLoader).from_string(template.email_template).render(bodyContext)
The first line works just fine, while the second line throws the Error:
AttributeError: 'Environment' object has no attribute 'get_source'
The context dictionary includes all necessary template variables and the jinja2 syntax should be correct as well. Just to be sure, here are the templates as well:
template.email_template:
Hallo {{name}},
{%- include invoiceType+'/email_body.tpl' -%}
Viele Grüße
{{emailFooter}}
invoiceType/email_body.tpl:
im Anhang findest du/findet ihr den Entwurf für die kommende Rechnung
über alle angefallenen Kosten bis zum {{invoiceDate -}}.
template.email_subject:
Rechnung {{currentYear}}{{invoiceID}}
I was able to resolve the issue by writing the string into a file and load it from the file.
template_dir = os.path.join(BASE_DIR, 'invoice\\templates')
loader = FileSystemLoader(template_dir)
environment = Environment(loader=loader)
body = environment.get_template("email.tpl").render(bodyContext)