I'm using FastAPI with Jinja2.
I have a function that sends out an email. The content of the email is inside a HTML that needs to be rendered.
async def send_confirmation_email(
self, email: str, confirm_url: str, request: Request
):
html = templates.TemplateResponse(
"email_template.html",
{
"request": request,
"header": "Thanks for joining",
"url": confirm_url,
"link_title": "ACTIVATE YOUR ACCOUNT",
},
)
await self.send_email(
html.template.render(),
"Confirm Your Email",
setting.EMAIL_SUPPORT,
email,
)
html.template.render() is only rendering the content of HTML without filling in the context into the placeholders. e.g. "header": "Thanks for joining" remains empty.
snippet from HTML:
<td align="center" width="120" colspan="3" valign="top"><h2>{{ header }}</h2></td>
What am I missing please?
TemplateResponse return a starlette.responses.Response, not a renderer template .
For that, you need to use
templates.get_template("email_template.html").render({
"request": request,
"header": "Thanks for joining",
"url": confirm_url,
"link_title": "ACTIVATE YOUR ACCOUNT",
})
(ref: https://github.com/encode/starlette/blob/master/starlette/templating.py#L72)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With