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.

Compatibility

At the time of writing this app is compatible with Django 4.2 and better (up to and including the Django main 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-3.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

django_js_asset-3.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_js_asset-3.0.0.tar.gz
Algorithm Hash digest
SHA256 4938404e09091f1da6d155868471e4831e6bcda18184769847a2822c5c09f240
MD5 b79fe94db7ee0021864f8c99108202fc
BLAKE2b-256 96df3157091edb33683e482f977bf15cecab6d7ad654be6544ce00e70050186d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_js_asset-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3f2295f424a3b7d62c5a2a4eea77805ea586b9fbdf083d9e4aa798996689bf1
MD5 bf3dca2df3e5e4587f557cb0eb3e59f6
BLAKE2b-256 997c708e557bc8b4a48c635cb35e12513e5f1661ab058ce13e9dca66265782a9

See more details on using hashes here.

Supported by

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