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

media.add_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.

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

Uploaded Source

Built Distribution

django_js_asset-0.1.1-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-js-asset-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c6637e4f3fef12024564057a48bef484db6491c610273d76d25d8dbe6ecc4ac1
MD5 8b0f04559463ee8b6eacd7fc722781f7
BLAKE2b-256 e77091802c80a0c7de49d9b8abf1b21d0a8eabdaf61f1a647e5899a0f105290d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_js_asset-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0dd2c5f64f2b24eb8a7270a6a59cb914a03f205335bd0eb6207bf61cf7410828
MD5 bd98771e4aa321737f3594d7816c3a5c
BLAKE2b-256 206f462444435476d2015cf54dd6eaa08447a566b586ff6e7fca19c2d4639e86

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