Django hyperscript
Django Hyperscript is a simple Django template tag library for dumping data into _hyperscript.
It’s a wrapper around hyperscript-dump, with Django-specific features like automatic model serialization and safe output for templates.
Installation
- Install using pip:
pip install django-hyperscript
- Add
django_hyperscripttoINSTALLED_APPSin your Django project'ssettings.py:
# settings.py
INSTALLED_APPS = [
...,
'django_hyperscript',
]
- Load the tag library in the necessary templates:
{% load hyperscript %}
Usage
By default, django-hyperscript wraps its output in a <div> with a class of hs-wrapper.
hs_dump
Dumps data into a single hyperscript variable.
{% hs_dump data 'myData' %}
assuming data is {"foo": "bar"}, the tag would output
<div class="hs-wrapper" _="
init
set global myData to {'foo': 'bar'}
then remove me
end"></div>
Using the kwarg flatten will assign the first layer of a dictionary's key value pairs into hyperscript variables. The name argument is negligible when using flatten.
{% hs_dump data flatten=True %}
assuming data is {"foo": "bar", "baz": "qux"}, the tag would output
<div class="hs-wrapper" _="
init
set global foo to 'bar'
set global baz to 'qux'
then remove me
end"></div>
Model Serialization
Django-hyperscript includes a built-in lightweight serializer to help convert Django Model instances and QuerySets into plain data structures for use in templates. This includes instances or querysets that are nested within dictionaries or lists, which will be serialized recursively.
By default, the serializer returns all editable fields defined on the model. However, you can customize this by adding an hs_fields attribute to your model, which is a list of field names you want exposed.
class Book(models.Model):
name = models.CharField(max_length=32)
year_published = models.SmallIntegerField()
hs_fields = ['name', 'year_published']
would return an instance as
{
"name": "foo",
"year_published": 1986
}
This serializer is intentionally minimal and does not support advanced features like relation traversal, nested serialization, field aliasing, or custom computed fields. If you need those capabilities, consider serializing your data ahead of time using Django REST Framework, or a custom serializer.
If needed, this serializer can be accessed via
from django_hyperscript.serializer import hs_serialize
Configuration
hs_dump supports all kwargs from hyperscript-dump's build_hyperscript along with some additional ones:
preserve
Type: bool | Default: False
Keeps the element the hyperscript is on in the DOM after initializing if True.
camelize
Type: bool | Default: True
"Camelizes" dictionary keys from snake case (snake_case) to camel case (camelCase) to fit JavaScript naming conventions.
flatten
Type: bool | Default: False
If True, each key value pair in a dictionary is assigned as a separate variable, rather than as a single object.
Note: Requires data to be a dictionary.
scope
Type: str | Default: global
Determines the scope of the hyperscript variable (global, element, or local).
event
Type: str, None | Default: init
Specifies the event that triggers assignment. The hyperscript "on" keyword should not need be provided. When set to none only the hyperscript assignment statements will be returned.
Note: If preserve is False (which it is by default), the element will not be removed until after the event is fired and values are set.
debug
Type: bool | Default: False
Logs the set variable name(s) and value(s).
wrap
Type: bool | Default: True
Wraps the hyperscript in a <div> with its display set to none if True, otherwise returns the raw hyperscript text.
class
Type: str | Default: hs-wrapper
Sets the HTML class/classes on the wrapper <div>.
Final example
{% hs_dump data 'my_data' preserve=True camelize=False scope='element' wrap=False %}
assuming data is {"my_value": 25}, the tag would output
"init set element my_data to {'my_value': 25} end"
In this example:
- The hyperscript remains in the DOM since
preserveisTrue - The keys within the dumped data remain in snake case since
camelizeisFalse - The variable is scoped to the element the hyperscript belongs to since
scopeis set to'element' - The output is just the raw hyperscript text since
wrapis set toFalse
License
This project is licensed under the MIT License - see the LICENSE file for details.
Release files for django-hyperscript 1.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_hyperscript-1.5.2.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_hyperscript-1.5.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.8 kB
Release files / django_hyperscript-1.5.2.tar.gz
| Download URL | django_hyperscript-1.5.2.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
31a3a11119ac23e6485bbca064b6e8a9e4e680e490b82e6010b561a4caf81959
|
|
BLAKE2b-256 checksum How to use checksums |
afcfbd1a2804f474b16ea6a22b8fc92109f71eb7f4bef5e12758c366ed7866a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.0
|
Release files / django_hyperscript-1.5.2-py3-none-any.whl
| Download URL | django_hyperscript-1.5.2-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2bd8a8d0b418b0edc691b65e945660ead30beb7169a2ca2b9ba19f23817e4e6f
|
|
BLAKE2b-256 checksum How to use checksums |
614796bdcf968fd40148c0a7eb13e73070e8fbb7af4746f7d72fc569bf1b055d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.0
|