Skip to main content

django-mjml-templates

A drop-in replacement for Django's template engine that also understands .mjml files.

uv add django-mjml-templates   # or pip install

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. A template whose name ends in .mjml (or a from_string whose code starts with <mjml) is a Django template first — {% extends %}, loops, {% url %}, autoescaping — and the MJML that produces is compiled to email HTML on render.

{# 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="{{ link }}">Get started</mj-button>
    </mj-column></mj-section>
  </mj-body>
</mjml>
from django.template.loader import get_template

get_template("email/welcome.mjml").render_email({"user": user, "link": link}, to=[user.email]).send()

render_email(context, request=None, *, subject="", **kwargs) returns an unsent EmailMultiAlternatives with the plain-text body and the HTML alternative attached, so you can still attach() a file before sending. kwargs go to EmailMultiAlternatives: to, from_email, bcc, reply_to, headers, and so on.

For the raw pieces, render_email_parts(context, request=None, *, subject="") returns a (subject, text, html) named tuple. Both are on .mjml templates only:

  • subject — as given, else the rendered <mj-title>, else ValueError.
  • textemail/welcome.txt rendered with the same context if it exists, else the HTML run through html2text.
  • html — the compiled email.

MJML is compiled with mjml-python (the Rust mrml port), so there is no Node dependency. Plain-text fallback uses html2text.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_mjml_templates-1.0.0.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_mjml_templates-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file django_mjml_templates-1.0.0.tar.gz.

File metadata

  • Download URL: django_mjml_templates-1.0.0.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_mjml_templates-1.0.0.tar.gz
Algorithm Hash digest
SHA256 15d670ab59f5e79376ea0633886fbaf3f4707ceb3db93fb611c4f2c209354676
MD5 0c4b1e95fb77f785bbe4486a832b7ff4
BLAKE2b-256 2eb924be5d6339c7dee5d1cfc8f2a20973bc3227b638104b5e79b52a5343f53b

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_mjml_templates-1.0.0.tar.gz:

Publisher: release.yml on SmileyChris/django-mjml-templates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_mjml_templates-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mjml_templates-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de196564e2a3a3b888256794258c2416ff7df0d292136c5db0ff31dc63e02ac4
MD5 9e51c2c9fbcf0454f42da274492af905
BLAKE2b-256 cf66724eec2964e725a337aedd4fec68dba8b7d6bdfbef6ef6806b2c6ac55934

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_mjml_templates-1.0.0-py3-none-any.whl:

Publisher: release.yml on SmileyChris/django-mjml-templates

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.1.0

2 files

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page