Hey there guys. :) I hope you're all well.
I have a Task that's supposed to render a context using Jinja2 in a HTML file.
I have something like this:
def write_clipping_email(script_directory):
# Scrapes for news
complete_news = get_news()
context = {"complete_news": complete_news}
# Needed for the tasks in PythonAnywhere (https://help.pythonanywhere.com/pages/ScheduledTasks)
news_filename = os.path.join(script_directory, "email_news.html")
# Required for Jinja2
environment = Environment(loader=FileSystemLoader(JINJA_ENV_TEMPLATES_PATH))
email_template = environment.get_template("news_html.html")
with open(news_filename, mode="w", encoding="utf-8") as news:
news.write(email_template.render(context))
print(f"... wrote {news_filename}")
return True
The script_directory
variable that is passed as an argument:
# Needed for the tasks in PythonAnywhere (https://help.pythonanywhere.com/pages/ScheduledTasks)
script_directory = os.path.dirname(os.path.abspath(__file__))
JINJA_ENV_TEMPLATES_PATH
is a environment variable with the full path for the templates.
In my log I do have this print:
... wrote /home/Gabrielsldev/gspwm_emails/main/email_news.html
The HTML file do show up in the folder, i.e., is written from the template.
But the template tags, like {{ variable }}, are not rendered. That is, it seems like the render(context)
is not working.
Do you guys have any idea about what may be happening?
Thanks a lot! :)