Skip to main content

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

Project description

Djact v3.0

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

Features

  • Write backend logic and HTML in the same template.
  • Full support for Python imports (from myapp.models import Task).
  • dj:component namespace isolation (multiple components per page).
  • Two-way data binding with dj:model.
  • Simple directives like dj:click, dj:submit, dj:extra.

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("djact/", include("djact.urls")),
]

3) Enable Middleware

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.

4) Template example (home.dj.html)

{% load djact %}

{% djact "todo" %}
# You can import models or anything else here!
# from myapp.models import Task

def mount(request):
    return {
        "tasks": [{"id": 1, "title": "Buy Milk"}, {"id": 2, "title": "Read Book"}],
        "new_title": "",
    }

def add_task(request, data):
    new_task = {"id": len(data["tasks"]) + 1, "title": data["new_title"]}
    tasks = data["tasks"]
    tasks.append(new_task)
    return {"tasks": tasks, "new_title": ""}

def delete_task(request, data):
    task_id = data["__extra"]["id"]
    tasks = [t for t in data["tasks"] if t["id"] != task_id]
    return {"tasks": tasks}
{% enddjact %}

<div dj:component="todo">
    <h1>Todos ([[ tasks.length ]])</h1>

    <form dj:submit="add_task">
        <input dj:model="new_title" placeholder="New task..." />
        <button type="submit">Add</button>
    </form>

    <ul>
        <li dj:for="task in tasks">
            [[ task.title ]] 
            <button dj:click="delete_task" dj:extra="id=task.id">Delete</button>
        </li>
    </ul>

    <p dj:empty="tasks">No tasks yet!</p>
</div>

Directives

  • dj:component="name": Links a DOM tree to a named Python component. Replaces dj:state.
  • dj:model="field": Two-way data binding for inputs.
  • dj:click="method": Calls a server method.
  • dj:submit="method": Calls a server method on form submit.
  • dj:extra="key=value": Passes extra payload data to the server.
  • dj:for="item in list": Repeats an element.
  • dj:if="condition": Conditionally toggles display.
  • dj:empty="list": Shows only if the list is empty.
  • dj:paginate="list": Automatically handles pagination limits.

Notes

  • mount() is mandatory for every component.
  • The Python code inside {% djact %} runs at request time with access to the full Django environment.

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-3.0.2.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

djact-3.0.2-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for djact-3.0.2.tar.gz
Algorithm Hash digest
SHA256 1cbafdc1c936d5c3868d7f6acf33eecd542a9bb88eaaf4ff053ea398a2be3eb9
MD5 fd520c6f8c08d5d281d3a7f1ab3e189f
BLAKE2b-256 de0836a7f120a3e7c1c89d1c3bf033043bf082f30659035cf7eeefcaf9f68b33

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for djact-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f097034c2b243905be3c99777229afcb39f82760d03e34f7ae081a72b0cf60bd
MD5 691823a1d3f2c8bcca4d9c7c5a3d01cf
BLAKE2b-256 961916cedba173cff96a85b9e72751216dbd3312d27a058fc35fe5512e199890

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