Skip to main content
Stand With Ukraine

test pypi

Django + MJML

django-mjml

The simplest way to use MJML in Django templates.


Installation

Requirements:

  • Django from 3.2 to 6.1

  • requests from 2.24.0 (only if you are going to use API HTTP-server for rendering)

  • mjml from 4.18.0 to 5.4.0 (older version may work, but not tested anymore)

1. Install mjml.

Follow https://github.com/mjmlio/mjml#installation and https://documentation.mjml.io/#installation to get more info.

2. Install django-mjml.

$ pip install django-mjml

If you want to use API HTTP-server you also need requests (at least version 2.24):

$ pip install django-mjml[requests]

To install development version use git+https://github.com/liminspace/django-mjml.git@main instead django-mjml.

3. Set up settings.py in your django project.

INSTALLED_APPS = (
  ...,
  'mjml',
)

Usage

Load mjml in your django template and use mjml tag that will compile MJML to HTML:

{% load mjml %}

{% mjml %}
    <mjml>
        <mj-body>
            <mj-section>
                <mj-column>
                    <mj-text>Hello world!</mj-text>
                </mj-column>
            </mj-section>
        </mj-body>
    </mjml>
{% endmjml %}

Advanced settings

There are three backend modes for compiling: cmd, tcpserver and httpserver.

cmd mode

This mode is very simple, slow and used by default.

Configure your Django:

MJML_BACKEND_MODE = 'cmd'
MJML_EXEC_CMD = 'mjml'

You can change MJML_EXEC_CMD and set path to executable mjml file, for example:

MJML_EXEC_CMD = '/home/user/node_modules/.bin/mjml'

Also you can pass addition cmd arguments, for example:

MJML_EXEC_CMD = ['node_modules/.bin/mjml', '--config.minify', 'true', '--config.validationLevel', 'strict']

Once you have a working installation, you can skip the sanity check on startup to speed things up:

MJML_CHECK_CMD_ON_STARTUP = False

tcpserver mode

This mode is faster than cmd but it needs the MJML TCP-Server.

Configure your Django:

MJML_BACKEND_MODE = 'tcpserver'
MJML_TCPSERVERS = [
    ('127.0.0.1', 28101),  # the host and port of MJML TCP-Server
]

You can set several servers and a random one will be used:

MJML_TCPSERVERS = [
    ('127.0.0.1', 28101),
    ('127.0.0.1', 28102),
    ('127.0.0.1', 28103),
]

httpserver mode

don’t forget to install requests to use this mode.

This mode is faster than cmd and a bit slower than tcpserver, but you can use official MJML API https://mjml.io/api or run your own HTTP-server (for example https://github.com/danihodovic/mjml-server) to render templates.

Configure your Django:

MJML_BACKEND_MODE = 'httpserver'
MJML_HTTPSERVERS = [
    {
        'URL': 'https://api.mjml.io/v1/render',  # official MJML API
        'HTTP_AUTH': ('<Application ID>', '<Secret Key>'),
    },
    {
        'URL': 'http://127.0.0.1:38101/v1/render',  # your own HTTP-server
    },
]

You can set one or more servers and a random one will be used.

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-1.6.tar.gz (8.8 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-1.6-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django_mjml-1.6.tar.gz.

File metadata

  • Download URL: django_mjml-1.6.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for django_mjml-1.6.tar.gz
Algorithm Hash digest
SHA256 134c51bdeb8f18de8ed05db2c917a4d1cc784130d318396dde2c470b620a9676
MD5 349cf43206b36d3891c58ba933171250
BLAKE2b-256 c4b32b76e5171a60cc374ad87f7b240d600d2f93de704d293f052402a9d30ae0

See more details on using hashes here.

File details

Details for the file django_mjml-1.6-py3-none-any.whl.

File metadata

  • Download URL: django_mjml-1.6-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for django_mjml-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0891b9dbe82625a3a68b9f41478f62e02973340f9f26225649002e22a5d264c5
MD5 3e464f740c02f2459818b15e3aa924be
BLAKE2b-256 d4ca2fce8cb6ea85e187f3921a4315375ebac9b04de9790cfb3b342a9dee4e11

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.6 This release

2 files

1.5

2 files

1.4

2 files

1.3

2 files

1.2

2 files

1.1

2 files

1.0

2 files

0.12.0

2 files

0.11.0

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.4

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

1 file

0.1.0

1 file

0.0.1

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