django-mjml-templates
A drop-in replacement for Django's template engine that also understands .mjml files,
so an email template is an ordinary Django template that happens to compile to email HTML.
MJML is compiled with mjml-python, the Rust mrml
port, so there is no Node dependency; the plain-text fallback uses
html2text.
Setup
uv add django-mjml-templates # or pip install
Then swap the backend; everything else about TEMPLATES stays as it was. The engine
is still named django (Django takes the default name from the module segment of
BACKEND), so engines["django"] and using="django" keep working.
TEMPLATES = [{
"BACKEND": "mjml_templates.django.DjangoTemplates", # was django.template.backends.django.DjangoTemplates
"APP_DIRS": True,
"OPTIONS": {"context_processors": [...]},
}]
Every other template renders exactly as before. Only a template whose name ends in
.mjml — or a from_string whose code starts with <mjml — is treated as email.
Writing an email
An .mjml file is a Django template first: {% extends %}, loops, {% url %} and
autoescaping all run as usual, and the MJML that produces is compiled on render. See
the MJML docs for the components themselves.
{# templates/email/welcome.mjml #}
<mjml>
<mj-head><mj-title>Welcome, {{ user.first_name }}</mj-title></mj-head>
<mj-body>
<mj-section><mj-column>
<mj-text>Hi {{ user.first_name }}, thanks for signing up.</mj-text>
<mj-button href="{% url 'start' %}">Get started</mj-button>
</mj-column></mj-section>
</mj-body>
</mjml>
<mj-title> doubles as the subject line. A sibling email/welcome.txt, if you write one,
becomes the plain-text body; without it the HTML is run through html2text.
Sending
from django.template.loader import get_template
get_template("email/welcome.mjml").render_email({"user": user}, request, to=[user.email]).send()
render_email(context, request=None, *, subject="", **kwargs) returns an unsent
EmailMultiAlternatives with both bodies attached, so you can still attach() a file
first. kwargs go straight to EmailMultiAlternatives: to, from_email, bcc,
reply_to, headers, and so on.
render_email_parts(context, request=None, *, subject="") gives the raw
(subject, text, html) named tuple instead. Both methods exist on .mjml templates only.
The subject is the one you pass, else the rendered <mj-title>, else a ValueError.
Absolute URLs
A relative link is dead in an email — there is no page for the mail client to resolve it
against. So every .mjml render rewrites href, src, background and CSS url()
against a base URL, and {% url %}, {% static %} and hand-written paths all come out
absolute. Anything already carrying a scheme, a protocol-relative //host/… or a bare
#fragment is left alone.
The base comes from the request if you pass one, and otherwise from MJML_BASE_URL:
MJML_BASE_URL = "https://example.com" # a full URL, used as-is
MJML_BASE_URL = "https://" # scheme only (the default) — paired with the current Site
The default means projects with django.contrib.sites installed need no setting at all.
With no request, no full URL and no sites app there is no base to work from, and URLs are
left relative rather than raising — so sending from a Celery task or a management
command wants one of the two. (A MJML_BASE_URL with no scheme at all is an
ImproperlyConfigured.)
The rewrite only reaches URLs in markup. Where a template writes one out as text, and in
the .txt sibling where there is no markup at all, use mjml_base:
{# email/welcome.txt #}
Sign up: {{ mjml_base }}{% url "signup" %}
Pass your own mjml_base in the context and it wins over the derived one.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_mjml_templates-1.1.0.tar.gz.
File metadata
- Download URL: django_mjml_templates-1.1.0.tar.gz
- Upload date:
- Size: 26.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d67bf74b24aabd841789dc0af4b0eaafff284e8fc9d6722c263e46aaae61ef7e
|
|
| MD5 |
46acf9db50155936a69c77cb924245c3
|
|
| BLAKE2b-256 |
63afd3400cc6d60b80775f50c5f4832df70c3d6962bd5cbb400bbde9f8740077
|
Provenance
The following attestation bundles were made for django_mjml_templates-1.1.0.tar.gz:
Publisher:
release.yml on SmileyChris/django-mjml-templates
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_mjml_templates-1.1.0.tar.gz -
Subject digest:
d67bf74b24aabd841789dc0af4b0eaafff284e8fc9d6722c263e46aaae61ef7e - Sigstore transparency entry: 2852939985
- Sigstore integration time:
-
Permalink:
SmileyChris/django-mjml-templates@154a97c3a3452193c6dffc4e12ebb1ea8ee05bba -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/SmileyChris
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@154a97c3a3452193c6dffc4e12ebb1ea8ee05bba -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_mjml_templates-1.1.0-py3-none-any.whl.
File metadata
- Download URL: django_mjml_templates-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec2853111e28d0f7866fefcf2c3c8f4a55c0ad64d8c9745af8502c80e98d009a
|
|
| MD5 |
78644049ee5830857e7196027a634618
|
|
| BLAKE2b-256 |
6819cd1064d258723daea6bdb5f670a23da35745e6d04638bb0c7d98da55f5d0
|
Provenance
The following attestation bundles were made for django_mjml_templates-1.1.0-py3-none-any.whl:
Publisher:
release.yml on SmileyChris/django-mjml-templates
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_mjml_templates-1.1.0-py3-none-any.whl -
Subject digest:
ec2853111e28d0f7866fefcf2c3c8f4a55c0ad64d8c9745af8502c80e98d009a - Sigstore transparency entry: 2852940014
- Sigstore integration time:
-
Permalink:
SmileyChris/django-mjml-templates@154a97c3a3452193c6dffc4e12ebb1ea8ee05bba -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/SmileyChris
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@154a97c3a3452193c6dffc4e12ebb1ea8ee05bba -
Trigger Event:
push
-
Statement type: