Skip to main content

Django ESM

Django ESM: NextGen JavaScript ESM module support for Django

NextGen JavaScript ESM module support for Django.

PyPi Version Test Coverage GitHub License

Sponsors

Sponsors

Highlights

  • 😌 easy transition
  • ⚡️ smart cache busting
  • 📦 no more bundling
  • ☕️ native ESM support
  • 📍 local vendoring with npm

Setup

Install the package and add it to your INSTALLED_APPS setting:

pip install django-esm[whitenoise]

First, add django_esm to your INSTALLED_APPS settings:

# settings.py
INSTALLED_APPS = [
    # …
    "django_esm",  # add django_esm before staticfiles
    "django.contrib.staticfiles",
]

Optionally: If you are using whitenoise you will need to modify your WSGI application.

import os
import pathlib

from django.core.wsgi import get_wsgi_application

from django_esm.wsgi import ESM

BASE_DIR = pathlib.Path(__file__).parent.parent

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

application = ESM(get_wsgi_application())

Finally, add the import map to your base template:

<!-- base.html -->
<!DOCTYPE html>
{% load esm %}
<html lang="en">
<head>
  <script type="importmap">{% importmap %}</script>
  <title>Django ESM is awesome!</title>
</head>
</html>

That's it! Remember to run npm install and python manage.py esm --watch.

Treeshaking

By default, every package entry point ends up in the import map. Pass --treeshake to drop entry points that are not reachable from your project's own entry points, keeping only the outputs actually imported:

python manage.py esm --watch --treeshake

Usage

You can now import JavaScript modules in your Django templates:

<!-- index.html -->
{% block content %}
  <script type="module">
    import "lit"
  </script>
{% endblock %}

Form.media

To use your importmap in Django forms, you can use the Form.media attribute:

# forms.py
from django import forms
from django_esm.forms import ImportESModule


class MyForm(forms.Form):
    name = forms.CharField()

    class Media:
        js = [ImportESModule("@sentry/browser")]

Now {{ form.media.js }} will render to like this:

<script type="module">import '@sentry/browser'</script>

Private modules

You can also import private modules from your Django app:

<!-- index.html -->
{% block content %}
  <script type="module">
    import "#myapp/js/my-module.js"
  </script>
{% endblock %}

To import a private module, prefix the module name with #. You need to define your private modules in your package.json file:

{
  "imports": {
    "#myapp/script": "./myapp/static/js/script.js",
    // You may use trailing stars to import all files in a directory.
    "#myapp/*": "./myapp/static/js/*"
  }
}

How it works

Django ESM works via native JavaScript module support in modern browsers. It uses the import map to map module names to their location on the server.

Here is an example import map:

{
  "imports": {
    "htmx.org": "/static/htmx.org/dist/htmx.min.js"
  }
}

Download files

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

Source Distribution

django_esm-0.9.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

django_esm-0.9.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file django_esm-0.9.1.tar.gz.

File metadata

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

File hashes

Hashes for django_esm-0.9.1.tar.gz
Algorithm Hash digest
SHA256 ff7eb53a444b20a06df6a21f10325c5e1955770768354aaf5694bb3a15b24b3f
MD5 e54c797d35855b7e8be90d730d0458e4
BLAKE2b-256 2d9b28d5c4fc472077e7f249e8b36249bfc2a059cc080a730ada621d430595e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_esm-0.9.1.tar.gz:

Publisher: release.yml on codingjoe/django-esm

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_esm-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: django_esm-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_esm-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 378bb82c423ebe1b2d491f82f19804d3d8ec606e02984b2b22d1a73eb3ca0bfc
MD5 63ef4395f8c35e1cae57b3b140a91302
BLAKE2b-256 574369445ee624483bd5a179a01d1c424401494dd2cea9e10613d783304bbea1

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_esm-0.9.1-py3-none-any.whl:

Publisher: release.yml on codingjoe/django-esm

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

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 files

0.9.0

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

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

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page