Use MJML in Django templates
Project description
django-mjml
The simplest way to use MJML in Django templates.
Installation
Requirements:
Django from 2.2 to 5.1
requests from 2.24.0 (only if you are going to use API HTTP-server for rendering)
mjml from 4.7.1 to 4.15.2 (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.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file django_mjml-1.3.tar.gz
.
File metadata
- Download URL: django_mjml-1.3.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/44.0 requests/2.28.2 requests-toolbelt/1.0.0 urllib3/1.26.19 tqdm/4.66.5 importlib-metadata/8.4.0 keyring/25.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6d2f6cf664f2f7d7e42e12dd1587a45545a7f43393b8dc1a574382975150516 |
|
MD5 | 58fd21d3f4d5dee68f8b56239ece958c |
|
BLAKE2b-256 | b109d4f32601d32579b521d89e3ceeadb9cce711b8b0d1b782f61b4c411b681a |
File details
Details for the file django_mjml-1.3-py3-none-any.whl
.
File metadata
- Download URL: django_mjml-1.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/44.0 requests/2.28.2 requests-toolbelt/1.0.0 urllib3/1.26.19 tqdm/4.66.5 importlib-metadata/8.4.0 keyring/25.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8eb15f32b6a9c497da48bff36e998b61dcf9bca2c44f3d98c7517e9edd06fe6 |
|
MD5 | b378ec6fa02c817c73ec84e7ffe002f7 |
|
BLAKE2b-256 | 9d789ecd1374525f06bda0aaaf8f3bb1b1a24a05e6b3d00d1ef344194dd8c4bd |