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.3.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.3-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: djact-3.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 bfd566044f0fc40d84eae2c0911e4e9147b9bc998c220c809772e0f63ce822b0
MD5 5bc1b8f4a4d2a16e65af7913e3878d11
BLAKE2b-256 c8d9eab178a89f365dcfe41da4e9d66edda7dbb85c985d88c1af9b54f40bd3c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djact-3.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6d32c7672f162032aa2554378b10284e026e14baffd87091ff2b20598f7126ec
MD5 d03fc3bea1bfd707d50989cde94889a5
BLAKE2b-256 657fd18d284952d2c5739534a41dfa7a758b00e2ccf3025749749c0f1ba4e069

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