edelvalle / djhtmx

Interactive UI components for Django using htmx.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

djhtmx

Interactive UI Components for Django using htmx

Install

Add djhtmx to your INSTALLED_APPS and install the Middleware as the last one of the list:

INSTALLED_APPS = [
    ...
    'djhtmx',
    ...
]

MIDDLEWARE = [
    ...,
    'djhtmx.Middleware',
]

And expose the HTTP endpoint in your urls.py as you wish, you can use any path.

from django.urls import path, include

urlpatterns = [
    # ...
    path('__htmx/', include('djhtmx.urls')),
    # ...
]

In your base template you need to load the necessary scripts to make this work

{% load htmx %}
<!doctype html>
<html>
    <head>
        {% htmx-headers %}
    </head>
</html>

Getting started

This app will look for live.py files in your app and registers all components found there, but if you load any module where you have components manually when Django boots up, that also works.

from djhtmx.component import PydanticComponent


class Counter(PydanticComponent):
    template_name = 'counter.html'
    counter: int = 0

    def inc(self, amount: int = 1):
        self.counter += amount

The counter.html would be:

{% load htmx %}
<div {% hx-tag %}>
  {{ counter }}
  <button {% on 'inc' %}>+</button>
  <button {% on 'inc' amount=2 %}>+2</button>
</div>

Now use the component in any of your html templates:

{% load htmx %} Counter: <br />
{% htmx 'Counter' %} Counter with init value 3:<br />
{% htmx 'Counter' counter=3 %}

What batteries are included

This project mixes htmx with morphdom for a more smooth rendering a find control of the focus when this one is on an input.

If you wanna use hx-boost go ahead and enable it with:

...
<body hx-boost="true">
   ...
</body>
...

Python APIs

TODO

Template tags

TODO

Extended htmx attributes

  • hx-after-swap: Add JavaScript here if you want it to be executed when the element is updated.

About

Interactive UI components for Django using htmx.org

License:MIT License


Languages

Language:JavaScript 81.7%Language:Python 15.3%Language:CSS 1.3%Language:HTML 0.9%Language:TypeScript 0.5%Language:Makefile 0.4%