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.4.tar.gz (12.1 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.4-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: djact-2.0.4.tar.gz
  • Upload date:
  • Size: 12.1 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.4.tar.gz
Algorithm Hash digest
SHA256 c8d74528b5fc052b179efbe2f26ba6d302c3b9b2f7c76846a4d4267d60f3cf31
MD5 ea330ec5b4270bf31db69fd10fe44b09
BLAKE2b-256 68008b499d83024499d3faf069ff8dcf8c1527d31f3237d0b063d563ae2175b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djact-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.3 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 edfb90bd76b73ba53fb16b8d4ca81ba1a02f74debaed268c8f82510270c34431
MD5 7caaf3ae3a1de1e14da115d21891fc08
BLAKE2b-256 a48a99ce5488c6a174353d9b42ec27fdf46b965ef7a07c6f5de4d6c8c070138b

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