Registry / web-framework / django-js-asset

django-js-asset

JSON →
library4.1.0pypypi✓ verified 30d ago

django-js-asset (current version 3.1.2) provides a `JS` object to insert script tags with additional HTML attributes into Django's `forms.Media` definitions, addressing a limitation in older Django versions. It also extends `forms.Media` to support `CSS` and `JSON` objects, allowing for more flexible asset management. The library is actively maintained and releases align with Django's compatibility requirements, supporting Django 4.2 and newer versions.

pip install django-js-asset
INSTALL
IMPORT
SIG · DJANGO-JS-ASSET
D
django-js-asset
web-frameworkpythonv4.1.0
Install
3.5s avg
Import
734ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v4.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.774s · 66.4MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.5s · import 0.694s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JS
✓ from js_asset import JS
CSS
✓ from js_asset import CSS
JSON
✓ from js_asset import JSON

Define a custom widget or form and declare your JavaScript, CSS, or JSON assets within its inner `Media` class using `JS`, `CSS`, and `JSON` objects. This allows for passing additional HTML attributes to script/style tags or embedding JSON data directly. The `forms.Media` is then rendered in your template using `{{ form.media }}`.

from django import forms from js_asset import JS, CSS, JSON # Example form demonstrating usage of JS, CSS, and JSON objects in Media class MyWidget(forms.TextInput): class Media: js = [ # Embed JSON data directly into a script tag JSON({"setting": "value", "number": 123}, id="my-widget-config"), # Include a JavaScript module with custom attributes JS("my_app/js/script.js", {"type": "module", "data-priority": "high"}), ] css = { "all": [ # Include an external stylesheet CSS("my_app/css/style.css"), # Include inline CSS directly CSS("p { color: blue; }", inline=True), ] } class MyForm(forms.Form): my_field = forms.CharField(widget=MyWidget) # In a Django template, you would render the media like this: # {{ form.media }}
Debug
Known issues
gotchaDjango 5.2+ introduces its own `Script` object for `forms.Media` with similar functionality. While `django-js-asset` offers a 'slightly different API' and extended features (like CSS/JSON objects and importmap support), users upgrading to Django 5.2 should be aware of the native option and choose the library that best fits their needs.
fix
Review Django 5.2 documentation for `forms.Media`'s native `Script` object. Decide whether to migrate to Django's native implementation or continue using `django-js-asset` for its extended features like CSS/JSON/Importmap handling and broader Django version support.
affects: Django >=5.2
deprecatedThe importmap support provided by `django-js-asset` is explicitly labeled as 'extremely experimental' and browser support for multiple importmaps is not generally available or standardized. Relying heavily on this feature in production is cautioned.
fix
Use the importmap feature with caution, particularly in production environments. Monitor browser compatibility and web standards for importmaps. Consider alternative asset bundling or management strategies for critical applications.
affects: All versions
gotchaWhen using the experimental importmap feature, if you add `importmap` to `forms.Media(js=[...])` and also include `{{ importmap }}` directly in your base templates (e.g., for Django admin), the importmap definition might be duplicated in the rendered HTML.
fix
To avoid duplication, either ensure that `{{ importmap }}` is only rendered if not already included via `form.media`, or implement different widget classes for admin vs. public site if the importmap setup differs.
affects: All versions (with importmap feature)
gotchaThe `static()` function's implementation can vary between Django versions, especially concerning `django.contrib.staticfiles`. `django-js-asset` provides its own `js_asset.static` helper to ensure correct static file URL resolution across supported Django versions.
fix
Always use `js_asset.static` when defining paths if you require compatibility across a wide range of Django versions or encounter issues with standard `django.contrib.staticfiles.static` resolving paths.
affects: All versions, especially older Django versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'js_asset'
The `django-js-asset` package is either not installed in the Python environment or not added to the `INSTALLED_APPS` list in your Django `settings.py` file.
fix
First, ensure the package is installed: `pip install django-js-asset`. Then, add `'js_asset'` to your `INSTALLED_APPS` list in `settings.py`.
ImportError: cannot import name 'JS' from 'js_asset'
You are trying to import the `JS` object (or `CSS`, `JSON`) from `js_asset` but there is a typo in the import statement, or the Python environment cannot find the module correctly.
fix
Correct the import statement to `from js_asset import JS` (or include `CSS`, `JSON` if needed, e.g., `from js_asset import JS, CSS, JSON`).
Uncaught ReferenceError: [function name] is not defined
JavaScript or CSS assets defined using `django-js-asset`'s `JS`, `CSS`, or `JSON` objects within `forms.Media` are not being rendered in your Django template, often because `{{ form.media }}` is missing or incorrectly placed.
fix
Ensure that `{{ form.media }}` is included in your Django template. For CSS, place it within the `<head>` section; for JavaScript, it's typically placed before the closing `</body>` tag.
NameError: name 'importmap' is not defined (in template) or ImportError: cannot import name 'importmap' from 'js_asset' (in Python)
The experimental `importmap` feature's context processor is not enabled in your Django settings, or the `importmap` object is not correctly imported in your Python code, leading to it being unavailable in templates or Python.
fix
For template usage, add `'js_asset.context_processors.importmap'` to `TEMPLATES['OPTIONS']['context_processors']` in your `settings.py` and ensure `{{ importmap }}` is present in your base template. For Python code, use `from js_asset import importmap`.
Upgrade
Version history
4.1.0latest on PyPI · released Aug 28, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
django-js-asset — pip install django-js-asset · libregistry