django-js-asset


Namedjango-js-asset JSON
Version 2.1.0 PyPI version JSON
download
home_pageNone
Summaryscript tag with additional attributes for django.forms.Media
upload_time2023-06-28 08:44:44
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseBSD-3-Clause
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===============================================================================
django-js-asset -- script tag with additional attributes for django.forms.Media
===============================================================================

.. image:: https://github.com/matthiask/django-js-asset/workflows/Tests/badge.svg
    :target: https://github.com/matthiask/django-js-asset

Usage
=====

Use this to insert a script tag via ``forms.Media`` containing additional
attributes (such as ``id`` and ``data-*`` for CSP-compatible data
injection.):

.. code-block:: python

    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:

.. code-block:: html

    <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``:

.. code-block:: javascript

    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 when using Django before 4.1 so specify
them as follows:

.. code-block:: python

    JS(
        "https://cdn.example.com/script.js",
        {"defer": "defer"},
        static=False,
    )


Compatibility
=============

At the time of writing this app is compatible with Django 1.8 and better
(up to and including the Django master branch), but have a look at the
`tox configuration
<https://github.com/matthiask/django-js-asset/blob/main/tox.ini>`_ for
definitive answers.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-js-asset",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Matthias Kestenholz <mk@feinheit.ch>",
    "download_url": "https://files.pythonhosted.org/packages/fa/82/80c8379233632fa803db84b8bafb11782f23fe5b59f27d8f6afcf26decc3/django_js_asset-2.1.0.tar.gz",
    "platform": null,
    "description": "===============================================================================\ndjango-js-asset -- script tag with additional attributes for django.forms.Media\n===============================================================================\n\n.. image:: https://github.com/matthiask/django-js-asset/workflows/Tests/badge.svg\n    :target: https://github.com/matthiask/django-js-asset\n\nUsage\n=====\n\nUse this to insert a script tag via ``forms.Media`` containing additional\nattributes (such as ``id`` and ``data-*`` for CSP-compatible data\ninjection.):\n\n.. code-block:: python\n\n    from js_asset import JS\n\n    forms.Media(js=[\n        JS(\"asset.js\", {\n            \"id\": \"asset-script\",\n            \"data-answer\": \"42\",\n        }),\n    ])\n\nThe rendered media tag (via ``{{ media.js }}`` or ``{{ media }}`` will\nnow contain a script tag as follows, without line breaks:\n\n.. code-block:: html\n\n    <script type=\"text/javascript\" src=\"/static/asset.js\"\n        data-answer=\"42\" id=\"asset-script\"></script>\n\nThe attributes are automatically escaped. The data attributes may now be\naccessed inside ``asset.js``:\n\n.. code-block:: javascript\n\n    var answer = document.querySelector(\"#asset-script\").dataset.answer;\n\nAlso, because the implementation of ``static`` differs between supported\nDjango versions (older do not take the presence of\n``django.contrib.staticfiles`` in ``INSTALLED_APPS`` into account), a\n``js_asset.static`` function is provided which does the right thing\nautomatically.\n\nWhen adding external script assets, you should pass ``static=False`` to the\n``JS`` object to avoid passing the script URL through ``static()``. In this\ncase, you probably want to add ``defer`` or ``async``, and maybe also\n``integrity`` and ``crossorigin`` attributes. Please note that boolean\nattributes are not properly supported when using Django before 4.1 so specify\nthem as follows:\n\n.. code-block:: python\n\n    JS(\n        \"https://cdn.example.com/script.js\",\n        {\"defer\": \"defer\"},\n        static=False,\n    )\n\n\nCompatibility\n=============\n\nAt the time of writing this app is compatible with Django 1.8 and better\n(up to and including the Django master branch), but have a look at the\n`tox configuration\n<https://github.com/matthiask/django-js-asset/blob/main/tox.ini>`_ for\ndefinitive answers.\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "script tag with additional attributes for django.forms.Media",
    "version": "2.1.0",
    "project_urls": {
        "Homepage": "https://github.com/matthiask/django-js-asset/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "6b97dc8601f87f22498a02c1183cf3d691079440b3c97704b327be778c55391e",
                "md5": "6fac051e8caadb78cef69641653bad41",
                "sha256": "36a3a4dd6e9efc895fb127d13126020f6ec1ec9469ad42878d42143f22495d90"
            },
            "downloads": -1,
            "filename": "django_js_asset-2.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6fac051e8caadb78cef69641653bad41",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4698,
            "upload_time": "2023-06-28T08:44:45",
            "upload_time_iso_8601": "2023-06-28T08:44:45.796618Z",
            "url": "https://files.pythonhosted.org/packages/6b/97/dc8601f87f22498a02c1183cf3d691079440b3c97704b327be778c55391e/django_js_asset-2.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "fa8280c8379233632fa803db84b8bafb11782f23fe5b59f27d8f6afcf26decc3",
                "md5": "eb2ad7f40dd78632c35eb4df08c4672c",
                "sha256": "be6f69ae5c4865617aa7726c48eddb64089a1e7d4ea7d22a35a3beb8282020f6"
            },
            "downloads": -1,
            "filename": "django_js_asset-2.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "eb2ad7f40dd78632c35eb4df08c4672c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 7734,
            "upload_time": "2023-06-28T08:44:44",
            "upload_time_iso_8601": "2023-06-28T08:44:44.215096Z",
            "url": "https://files.pythonhosted.org/packages/fa/82/80c8379233632fa803db84b8bafb11782f23fe5b59f27d8f6afcf26decc3/django_js_asset-2.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-28 08:44:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "matthiask",
    "github_project": "django-js-asset",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-js-asset"
}
        
Elapsed time: 0.09623s