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.2.tar.gz (12.0 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.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: djact-2.0.2.tar.gz
  • Upload date:
  • Size: 12.0 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.2.tar.gz
Algorithm Hash digest
SHA256 9aa49ab2fa13341fb61fac321deecd82049981d4d96372d17a8aed2cd46c3053
MD5 0196afa3ee9338c29382445b849ec7dc
BLAKE2b-256 9154e7f96d31293acae2e7f3037139b8e06c3a0f80966d9646f13d130c399f46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djact-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e139958b6c0894fed54983c91a5e9c8aeec7d4a8a2c96e32cf372f04e56a8065
MD5 b4605f6b7bdea06347a6d6c9161cd38e
BLAKE2b-256 2da11d5abd4b6467f7b0da3e7f745d79b00dc2992e927cc194139101bce7f801

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