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.
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
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-1.0.0.tar.gz
.
File metadata
- Download URL: django-js-asset-1.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38fc1de7a93a2f667fe7ee34acc53d682bac5ce6c73ac6f0ecb4ff77d0f61f1b |
|
MD5 | 53604761bc1efeab51f7124cffae86b8 |
|
BLAKE2b-256 | 10b34f767ec7d42c836c6deacd4f6118f5657f245dd8e9c21af84ec6bc929cb5 |
File details
Details for the file django_js_asset-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_js_asset-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcd00d7dc67f86cad1dac4bf7b43fa4b75088c214d2c92002b8705c6a7362e7e |
|
MD5 | ca8f82204ff0e60cfe44fcf8b07ad24c |
|
BLAKE2b-256 | 311793ae0759d3043711e1f3c8187d44d28d57cffb781aadc6e786710350e7d2 |