Skip to main content

HTML-first frontend interaction for Django, JSON-only server, and client-side DOM updates.

Project description

Djact

Djact is a lightweight HTML-first interaction system for Django. It is not React, not Inertia, and not Livewire. The server returns JSON only, and the client updates the DOM.

Why Djact

  • Write backend logic and HTML in the same template.
  • Simple directives like dj:click and dj:submit.
  • No HTML re-rendering from server.
  • A tiny client runtime that updates DOM based on state.

Installation

pip install djact

Quick Start

1) Add djact to INSTALLED_APPS

INSTALLED_APPS = [
    # ...
    "djact",
]

2) Include Djact URLs

from django.urls import path, include

urlpatterns = [
    # ...
    path("", include("djact.urls")),
]

3) Enable auto-loading for .dj.html

Add the middleware so Djact assets auto-load for templates ending with .dj.html:

MIDDLEWARE = [
    # ...
    "djact.middleware.DjactAutoLoadMiddleware",
]

Now create templates with .dj.html extension, and you do NOT need to manually load JS.

4) Template example (home.dj.html)

{% load djact %}

{% djact %}

def mount(request):
    return {
        "name": "",
        "showModal": False,
        "message": "",
    }


def increment(request, data):
    return {
        "name": "Ankur",
    }


def saveUser(request, data):
    return {
        "message": "Saved Successfully",
    }

{% enddjact %}

<div dj:state="name='', showModal=false, message=''">
    <h1>[[ name ]]</h1>

    <button dj:click="increment">Increment</button>

    <button dj:function="setState(showModal=true)">
        Open Modal
    </button>

    <form dj:submit="saveUser">
        <button type="submit">Save</button>
    </form>

    <p>[[ message ]]</p>
</div>

<script>
    window.methods = window.methods || {};
    window.methods.saveUser = async (state, setState) => {
        // client-only method example
        setState({ message: "Client Saved" });
    };
</script>

Directives

dj:state

Initial state for the component.

<div dj:state="name='', showModal=false"></div>

dj:click

Calls a server method.

<button dj:click="increment">Increment</button>

dj:submit

Handles form submit and calls server method.

<form dj:submit="saveUser">
  <button type="submit">Save</button>
</form>

dj:function

Calls client methods or setState().

<button dj:function="setState(showModal=true)">Open</button>

Example Project

A minimal working page is possible with only one template and a view-less setup because Djact uses the /djact endpoint for actions.

Comparison

  • Livewire: server re-renders HTML. Djact returns JSON only.
  • HTMX: HTML fragments. Djact uses JSON and client-side rendering.
  • Alpine: client-only. Djact adds optional server actions.

Folder Structure

djact/
├── __init__.py
├── apps.py
├── views.py
├── parser.py
├── registry.py
├── urls.py
├── templatetags/
│   └── djact.py
├── static/
│   └── djact/
│       ├── core.js
│       ├── state.js
│       ├── renderer.js
│       ├── renderer_expr.js
│       ├── directives.js
│       └── api.js

Notes

  • All server responses are JSON only.
  • mount() runs on page load.
  • All methods are plain Python functions.

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

djact-2.0.5.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

djact-2.0.5-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file djact-2.0.5.tar.gz.

File metadata

  • Download URL: djact-2.0.5.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for djact-2.0.5.tar.gz
Algorithm Hash digest
SHA256 efc22369cdd2469cbc35308722c63373d10ed853fcc9314b9a11c10abf9cf041
MD5 932cced42a5a4ae407d8fcded8d26718
BLAKE2b-256 18203a233c661ff99805221955ee547a887be283c35812879650d56ef780e244

See more details on using hashes here.

File details

Details for the file djact-2.0.5-py3-none-any.whl.

File metadata

  • Download URL: djact-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for djact-2.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 48c8468982e4b2b8dc9b733ac67e59f8a31705e814f1d98c991209e683bd4544
MD5 1c7e305ca4ba7be8e858a07d6058092c
BLAKE2b-256 62be20136d20ebbeacf2981ed9c5f37a24ce59b729bd3a8b2994e5066fb5270f

See more details on using hashes here.

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