Trigger HTMX events usign Django Channels and websockets
An example project can be found at https://github.com/paulik123/django-htmx-live-trigger-example
This app assumes you have working Django + Channels project.
If you don't know how to set it up, you can follow this tutorial:
https://channels.readthedocs.io/en/latest/tutorial/part_1.html
Installation:
pip install django-htmx-live-trigger
Then add django_htmx_live_trigger to you INSTALLED_APPS
INSTALLED_APPS = [
...,
"django_htmx_live_trigger",
]
Then add HTMXEventsConsumer to your websocket routing in asgi.py
from django.urls import re_path
from django_htmx_live_trigger.consumers import HTMXEventsConsumer
...
application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter([
# ...
re_path(r"ws/events/(?P<group_name>\w+)/$", HTMXEventsConsumer.as_asgi()),
])
)
),
}
)
Usage:
- Add
<script src="{% static 'django_htmx_live_trigger/js/htmx-ws-events.js' %}"></script>at the end of your body (after htmx). - Add the
hx-ext="ws-events"andws-events-connect="/ws/events/<group_name>/"attributes to your<body>.
Then, wherever you want in you project, you can trigger the events like so:
from django_htmx_live_trigger import trigger
trigger("<group_name>", "<event-name>")
You can also trigger multiple events at once like so:
from django_htmx_live_trigger import trigger
import json
trigger("<group_name>", json.dumps({"event1": "value1", "event2": "value2"}))
Example
...
<body hx-ext="ws-events" ws-events-connect="/ws/events/notifications/">
<div hx-get="{% url 'fetch_notifications' %}" hx-trigger="new-notification from:body"></div>
<script src="https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js"></script>
<script src="{% static 'django_htmx_live_trigger/js/htmx-ws-events.js' %}"></script>
</body>
...
from django_htmx_live_trigger import trigger
class Notificaiton(models.Model):
...
def save(self, *args, **kwargs):
is_new = self.pk is None
super().save(*args, **kwargs)
if is_new:
trigger("notifications", "new-notification")
Metadata
Release files for django-htmx-live-trigger 0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_htmx_live_trigger-0.6.tar.gz | 8.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_htmx_live_trigger-0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.4 kB
Release files / django_htmx_live_trigger-0.6.tar.gz
| Download URL | django_htmx_live_trigger-0.6.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
99b4b3cca6a0c3a9c4a9d5b1741bc1047070cd354f9d10ce9922c81085713c00
|
|
BLAKE2b-256 checksum How to use checksums |
628efdd1831a0168eed61948bcace00e68eb275cbb80fd2d1c4806ee3d920682
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.2
|
Release files / django_htmx_live_trigger-0.6-py3-none-any.whl
| Download URL | django_htmx_live_trigger-0.6-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a0f4ffdec39a11839958c01a990159061a4431930b453c9d60072509aeac7e5
|
|
BLAKE2b-256 checksum How to use checksums |
517b5e661dbfd2ad0c6913d66ee55fe1db4456996dbb8d4a756c3db35e620b81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.2
|
Security for django-htmx-live-trigger 0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.