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.0.tar.gz (20.6 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.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: djact-3.0.0.tar.gz
  • Upload date:
  • Size: 20.6 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.0.tar.gz
Algorithm Hash digest
SHA256 532f19da49ecd17cbae1cd1c4bac7a43d507abbb6d4c05b48f5e6b63567addbf
MD5 d494532ef6c6ca67f6de2e3684e5bd14
BLAKE2b-256 3a8bced41b40aa1db4b033fc26efebc950b8debd501f36e1b4c6b03ca7376ad7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: djact-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 23.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67cd05796213d643701de0d15947384c30e3393cdf684245c06cb45b2d51abf1
MD5 4aeb61cdf37ff747ea2c9fbfd2cc889b
BLAKE2b-256 0f6a3f44c0c0a80c24756f45d55bc88dcfe1c4c4f29494ab0ddfa23b66637a1f

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