Skip to main content

script tag with additional attributes for django.forms.Media

Project description

https://github.com/matthiask/django-js-asset/workflows/Tests/badge.svg

Usage

Use this to insert a script tag via forms.Media containing additional attributes (such as id and data-* for CSP-compatible data injection.):

from js_asset import JS

forms.Media(js=[
    JS("asset.js", {
        "id": "asset-script",
        "data-answer": "42",
    }),
])

The rendered media tag (via {{ media.js }} or {{ media }} will now contain a script tag as follows, without line breaks:

<script type="text/javascript" src="/static/asset.js"
    data-answer="42" id="asset-script"></script>

The attributes are automatically escaped. The data attributes may now be accessed inside asset.js:

var answer = document.querySelector("#asset-script").dataset.answer;

Also, because the implementation of static differs between supported Django versions (older do not take the presence of django.contrib.staticfiles in INSTALLED_APPS into account), a js_asset.static function is provided which does the right thing automatically.

When adding external script assets, you should pass static=False to the JS object to avoid passing the script URL through static(). In this case, you probably want to add defer or async, and maybe also integrity and crossorigin attributes. Please note that boolean attributes are not properly supported when using Django before 4.1 so specify them as follows:

JS(
    "https://cdn.example.com/script.js",
    {"defer": "defer"},
    static=False,
)

Compatibility

At the time of writing this app is compatible with Django 1.8 and better (up to and including the Django master branch), but have a look at the tox configuration for definitive answers.

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

django_js_asset-2.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

django_js_asset-2.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django_js_asset-2.1.0.tar.gz.

File metadata

  • Download URL: django_js_asset-2.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for django_js_asset-2.1.0.tar.gz
Algorithm Hash digest
SHA256 be6f69ae5c4865617aa7726c48eddb64089a1e7d4ea7d22a35a3beb8282020f6
MD5 eb2ad7f40dd78632c35eb4df08c4672c
BLAKE2b-256 fa8280c8379233632fa803db84b8bafb11782f23fe5b59f27d8f6afcf26decc3

See more details on using hashes here.

File details

Details for the file django_js_asset-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_js_asset-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36a3a4dd6e9efc895fb127d13126020f6ec1ec9469ad42878d42143f22495d90
MD5 6fac051e8caadb78cef69641653bad41
BLAKE2b-256 6b97dc8601f87f22498a02c1183cf3d691079440b3c97704b327be778c55391e

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