Skip to main content

Django HTMX Integration

Project description

Djxi | HTMX 4 Integration for Django

CI pre-commit Lint codecov

PyPI PyPI - Wheel PyPI - Python Version PyPI - Django Version


Stop scrolling for scattered HTMX!

Djxi lets you architect your HTMX widgets in one single Endpoint Battery. It bundles the urls, view logic, and the HTML into a central hub. The feature lives in one place — without scattering your code across urls.py, views/, and templates/.

  • No more archaeology. No more digging through three files just to tweak a button label.
  • Locality of Behaviour Request → Logic → Render stays in one class.
  • Scales cleanly. Small partials stay manageable, without turning your project into spaghetti.
  • Django Integration. Tag, Filters, Messages, HX-Headers, CBV and more are all integrated into Django.

📦 What is this?

Just a prenup between Grandpa Django and his sexy new HTMX fling — preventing scatterbrain syndrome and reactive dysfunction.

Django's Request-Render-Response cycle was architected with full page reloads in mind. The separation into views, urls and templates is practical when the response affect the whole of the client's state.

HTMX introduces minute partial updates via server-side rendered html snippets which update the page selectively and asynchronly. Those small page updates have to be orchestrated and maintained, each with its own view, url and template.

Using Django with HTMX usually results in a scattering of a multitude of template snippest, view logics and url endpoints.

Consider a simple CRUD Todo List: that is 4 urls, 4 views and 5 templates, if you do it with HTMX and create a partial for a todo item. This count can easily go up, as soon as the urge to allow in-place smart actions is given in. The number is not the problem it is the scattering of those snippets (url, view, html) over the codebase under vanilla Django best practices.

Therefor the marriage of Django and HTMX can be bad news for "Locality of Behaviour" and affect maintability of projects the more it make use of HTMX.

Djxi's solution:

Bundle HTMX urls, views and template collection all into one or more DXEndpointBattery. Here the feature 'todo-list' with all it's actions is described in full and obious at a glace in one central place.

from djxi import DXEndpointBattery, dx_action 

INLINE_TEMPLATE = """
<dx-section name="todo-list">
    <div hx-get={% url "load-todo-list" %} hx-trigger="load delay:100ms">
        <h3>TODO</h3>
        <div id="todo-list"></div>
    </div>
</dx-section>

<dx-section name="todo-item">
    <button disabled>Confirmed!</button>
</dx-section>
"""


class SimpleInlineActionRouter(DXEndpointBattery):
    inline_template = INLINE_TEMPLATE

    @dx_action("get-confirm-button", methods=["GET"])
    def get_confirm_button(self, request):
        context = {"name": "Phil"}
        return self.render_section(
            request, section_name="confirm-button", context=context
        )

Djxi is a opinionated yet frictionless HTMX drop-in. It can be run in parallel to vanilla Django views and even alongside the way you used to use HTMX.

Drop Djxi in and start streamline new or old HTMX Widgets.

Inline Templates?

HTML in a multiline string? Bäh, I loose all the template syntax higllighting! No problem at all, just use the template_name with a path to your template instead of the inline template. You can deal with two files per feature set.

Spice up Django CBV

Integrate with django.contrib.messages

Expose HX-Headers per middleware

Pre-Alpha Note

The package is considered in experimental pre-alpha state, use with caution.

  • Watch out for updates and consider giving it a star.
  • Checkout the djxi showcases in the example django app.

Getting Started

Instalation

  1. Install with pip:

python -m pip install djxi

  1. Add django-htmx to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...,
    "djxi",
    ...,
]
  1. Optional: Adjust your base template to get you up and running instantly
 {% load djxi %}
 <!doctype html>
 <html>
   <head>
     ...
     {% htmx_script_inclusion %}
   </head>
   <body {% htmx_headers %}>
     ...
   </body>
 </html>

The htmx_script_inclusion tag will pull the unminified v4 from CDN. Set DX_HTMX_VERSION="2" to pull in v2. For prodution you likely want to serve your own minified htmx.js.

As there are significant syntax changes between v4 and v2 of htmx, keep DX_HTMX_VERSION in sync with what htmx version you are serving.

Configuration

In your settings file you can overide the following default values for Djxi:

  • DX_HTMX_VERSION": "4" # Choose between 4 and 2
  • DX_HTMX_MINIFIED": False # Load a minified source, recommended for production

Quick start

Create and manage your HTMX Endpoint in a convenient Battery:

from djxi.actions import DXEndpointBattery, dx_action 

INLINE_TEMPLATE = """
<dx-section name="confirm-button">
    <button hx-post="/showcase/simple/confirm">
        Confirm, {{ name }}
    </button>
</dx-section>

<dx-section name="check-confirmed">
    <button disabled>Confirmed!</button>
</dx-section>
"""


class SimpleInlineActionRouter(DXEndpointBattery):
    inline_template = INLINE_TEMPLATE

    @dx_action("get-confirm-button", methods=["GET"])
    def get_confirm_button(self, request):
        context = {"name": "Phil"}
        return self.render_section(
            request, section_name="confirm-button", context=context
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djxi-0.1.5.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djxi-0.1.5-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file djxi-0.1.5.tar.gz.

File metadata

  • Download URL: djxi-0.1.5.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for djxi-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0889f99d7c14f716dcf65975029a5d0c38f8bebfe9a82f956afbba0857a86d0e
MD5 c0805c2701898ddb9742ce7c1d626823
BLAKE2b-256 ef399239829be25fbf8139c48d58a1b8270639609728435ddfe6a3f23e370152

See more details on using hashes here.

Provenance

The following attestation bundles were made for djxi-0.1.5.tar.gz:

Publisher: main.yml on rollinger/djxi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djxi-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: djxi-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for djxi-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 57e2db430f7795a6863ef122890b720b87b00c6255839aae97746b59b779faac
MD5 3fb385da54bc1a63dfaebe6209e68b5e
BLAKE2b-256 a4728eb0a12feb64cef7b694094c460261733b528dc01d2cfbd70a6931853354

See more details on using hashes here.

Provenance

The following attestation bundles were made for djxi-0.1.5-py3-none-any.whl:

Publisher: main.yml on rollinger/djxi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page