No description, website, or topics provided.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
js_asset Format the code using black Nov 10, 2018
tests Format the code using black Nov 10, 2018
.travis.yml Add Django 2.1 to the test matrix Nov 10, 2018
CHANGELOG.rst Format the code using black Nov 10, 2018
LICENSE Initial commit Apr 19, 2017 Initial commit Apr 19, 2017
setup.cfg Format the code using black Nov 10, 2018 Format the code using black Nov 10, 2018
tox.ini Format the code using black Nov 10, 2018


django-js-asset -- script tag with additional attributes for django.forms.Media


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

    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:

    {"defer": "defer"},


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.