Skip to main content

script tag with additional attributes for django.forms.Media

Project description

https://travis-ci.org/matthiask/django-js-asset.svg?branch=master

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, 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.7 and better (up to and including the Django master branch), but have a look at the Travis CI build 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-1.2.3.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

django_js_asset-1.2.3-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-js-asset-1.2.3.tar.gz.

File metadata

  • Download URL: django-js-asset-1.2.3.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django-js-asset-1.2.3.tar.gz
Algorithm Hash digest
SHA256 25811cd67428ebdc5298661a2223a66b39c11696746a06f87f0896b93e1c5423
MD5 5c5eaf3e1ebd1d499477162fb3993bd2
BLAKE2b-256 2db1d4cee9c2b85526576d1c766597400777b70e371fff1605c48a567d1ef096

See more details on using hashes here.

File details

Details for the file django_js_asset-1.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: django_js_asset-1.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django_js_asset-1.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4675015a390fd1f3937e79f7f516d58ab77572b0339c6873807555d6443fa9f8
MD5 cfe3a6b2561ec0fedd0ff828c0c17161
BLAKE2b-256 72ea9b0d8c65a7d67f98a6a1acb9e0d884d12a9520c9a9883ed6791e9deb06e5

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