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.

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.0.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

django_js_asset-1.0.0-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-js-asset-1.0.0.tar.gz
Algorithm Hash digest
SHA256 38fc1de7a93a2f667fe7ee34acc53d682bac5ce6c73ac6f0ecb4ff77d0f61f1b
MD5 53604761bc1efeab51f7124cffae86b8
BLAKE2b-256 10b34f767ec7d42c836c6deacd4f6118f5657f245dd8e9c21af84ec6bc929cb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_js_asset-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bcd00d7dc67f86cad1dac4bf7b43fa4b75088c214d2c92002b8705c6a7362e7e
MD5 ca8f82204ff0e60cfe44fcf8b07ad24c
BLAKE2b-256 311793ae0759d3043711e1f3c8187d44d28d57cffb781aadc6e786710350e7d2

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