Skip to main content

Integrate HTML importmaps with Django for modern web development

Project description

from dj_importmap.settings import INSTALLED_APPS

dj-importmap: HTML importmaps like a boss!

dj-importmaps is designed to help you declare your importmaps in a djangonic way.

Show me an example!

For instance, the following:

# importmaps.py in one of your Django apps
from importmap import static

importmaps = {
    # From your static files
    "SearchComponent": static("js/search-component.js"),
    # Or declare directly from a CDN
    "StimulusJS": "https://unpkg.com/stimulus@3.2.2/dist/stimulus.umd.js"
}

This will generate the following:

<script type="importmap">
  {
    "imports": {
        "SearchComponent": "/static/js/search-component.js",
        "StimulusJS": "https://unpkg.com/stimulus@3.2.2/dist/stimulus.js"
    }
  }
</script>

And now, you can use JS modules like nothing:

import {Controller, Application} from "StimulusJS"
import * as Search from "SearchComponent"

export default class {
    // ...
}

Cool! How do I use it?

  1. Install from PyPI

    pip install dj-importmap
    
  2. Add to your `INSTALLED_APPS:

    INSTALLED_APPS = [
        # ...
        "importmap"
        # ...
    ]
    
  3. Create a importmaps.py next to your root urls.py or in any of your Django app:

    importmaps = {
        # ...
    }
    
  4. Add {% importmap %} to you template:

    {% load importmap %}
    <!doctype html>
    <html lang="fr" data-fr-scheme="light">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title></title>
      <!-- This must be placed before the very first <script> in order to work -->
      {% importmap %}
    </head>
    </html>
    

You're good to go!

Advanced usage

Per-app importmaps.py

You can declare an different importmap for your whole Django project as well as for each application. By default, when you call {% importmap %} in a template, dj-importmap will merge them all in the order the app were declared in INSTALLED_APPS, the latter take precedence over the former.

If you want to use a importmaps.py declared in a spcific app, you can use {% importmap "app_name" %} where app_name corresponds to the AppConfig.name declared in your Django's app apps.py. In this case, {% importmap %} will merge the app's importmaps.py to the project importmaps.py, if exists.

dj-importmap expects to find the project's importmaps.py next to the project's urls.py, as declared in settings.ROOT_URLCONF.

Alternatively, if settings.ROOT_IMPORTMAPCONF is declared and points to a valid Python module, dj-importmap will source that one as the project's root importmap.

Additionnal HTML attribute to the generated <script>

{% importmap %} accepts kwargs to let you add arbitrary HTML attributes to the generated <script>:

{% importmap defer="true" %}
<!-- Using attributes with chars unauthorized in Django templates -->
{% importmap defer="true" "yes" as "data-is-cool" %}
<!-- Attribute without value -->
{% importmap defer="true" "yes" as "data-is-cool" %}

Note: trying to specify an HTML attribute with no value as the first argument of {% importmap %} creates an ambiguity with the optionam app name. In this case, {% importmap %} will raise a TemplateSyntaxError. {% importmap %} offer the possibility to suppress this ambiguity by setting a series of dashes as its first argument:

<!-- TemplateSyntaxError: ambiguity -->
`{% importmap %}`
<!-- Ok -->
{% importmap "--" defer %}

Project details


Download files

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

Source Distribution

dj_importmap-0.0.1.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

dj_importmap-0.0.1-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file dj_importmap-0.0.1.tar.gz.

File metadata

  • Download URL: dj_importmap-0.0.1.tar.gz
  • Upload date:
  • Size: 42.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for dj_importmap-0.0.1.tar.gz
Algorithm Hash digest
SHA256 36b7fcca1d597b083f8301b8aaf45336a40a14d263c9926da0859de0a442e481
MD5 d0bf5d1e32dfe0134de4a2097cff0932
BLAKE2b-256 91bda54fd2986b448eead5d171b9f1918df5eda68bc40f4deaef9ff34ae8f27f

See more details on using hashes here.

File details

Details for the file dj_importmap-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dj_importmap-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4da810c849d2764d93abf321a3ac064a016e75d367fdf70d3e04d4530d73001e
MD5 3843bc5f7068e4ec7e00025c1d5aea3c
BLAKE2b-256 f7c974cf2f350f1bc6ace029ae9a8ed92ac6dc14094936f3dfc0f1ec04390177

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page