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="&quot;42&quot;" 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.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

django_js_asset-1.1.0-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-js-asset-1.1.0.tar.gz
Algorithm Hash digest
SHA256 30149158206f693a5d027fe590096fc84495486bd11cd77d395b4f2ec27fc1d0
MD5 360cd06bce3a85620a0375a6aecf0f77
BLAKE2b-256 5531bc09d2c62bdd27cebd2bf1071fe000283b2d995da7af59c9640c5cfb7c04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_js_asset-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a395d8d19eb201ea8d2bd4f145b38f1717cd74c0f609f040141d8724c5a27f36
MD5 fd119ac7b2d7e7e36572c02c419deb8b
BLAKE2b-256 d596f68c636ebedc59576c3702b2bd00cd01eb8557a4b13d50c8fbc0cf91cc47

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