script tag with additional attributes for django.forms.Media
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_js_asset-2.0.0.tar.gz
.
File metadata
- Download URL: django_js_asset-2.0.0.tar.gz
- Upload date:
- Size: 4.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | adc1ee1efa853fad42054b540c02205344bb406c9bddf87c9e5377a41b7db90f |
|
MD5 | 99d75478fddf37e677137640874977c2 |
|
BLAKE2b-256 | 99a706be13db4030fff013137f0ccd6a2f46babb623b536eec6068430e176fb1 |
File details
Details for the file django_js_asset-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_js_asset-2.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86f9f300d682537ddaf0487dc2ab356581b8f50c069bdba91d334a46e449f923 |
|
MD5 | f406ec406d7d41d401790d07479019af |
|
BLAKE2b-256 | 89dd67936526e547dd14ca4e5d13ab1655bc4aefa410f092ad13de3a085a3d51 |