Member-only story

Simple Steps to Send Emails with Static Images in Django

Diego Carrasco G.
3 min readJan 9, 2024

Context

I wanted to send an email with a template that included an image using send_mail from Django. The image was stored in the static folder of the Django project. I wanted to use the Django template system to generate the email content, but I was not sure how to reference the image in the template.

Requirements

I assume you have a Django project with a static folder and a template that you want to use to generate an email.

Issue with {% static %} in Emails

The first thing I tried was to use the {% static %} template tag to generate the URL for the image. However, this tag does not generate a full URL, but a relative URL. This is not a problem when the template is rendered in the browser, but emails need a full URL to access the image.

This means that {% static %} only appends the relative path to the STATIC_URL, which is not sufficient for email clients to locate the image.

Solution — Generating Full URLs in the View (with code snippets)

The solution I found was to generate the full URL in the view and pass it as a context variable to the template.

--

--

Diego Carrasco G.
Diego Carrasco G.

Written by Diego Carrasco G.

Hi, I'm an entrepreneur-turned-developer living in Germany. I enjoy learning, write, coffee and solving problems. I write mainly about technology.

No responses yet