Building Your First Reactive Component with djust
What We'll Build
In this tutorial, we'll build a real-time counter component that updates without page refreshes. Simple, but it demonstrates the core concepts of djust.
Step 1: Create the View
First, create a new LiveView:
# views.py
from djust import LiveView
class CounterView(LiveView):
template_name = "counter.html"
def mount(self, request):
"""Initialize component state."""
self.count = 0
def increment(self):
"""Handle increment button click."""
self.count += 1
def decrement(self):
"""Handle decrement button click."""
self.count -= 1
Step 2: Create the Template
Create the template with event bindings:
<!-- counter.html -->
<div class="counter">
<h1>Count: {{ count }}</h1>
<button dj-click="decrement">-</button>
<button dj-click="increment">+</button>
</div>
Step 3: Add the URL
# urls.py
from django.urls import path
from .views import CounterView
urlpatterns = [
path("counter/", CounterView.as_view(), name="counter"),
]
That's It!
No JavaScript. No API endpoints. No state management libraries. Just Python and HTML.
When you click the buttons, djust:
- Sends the event to the server
- Runs your Python handler
- Computes the DOM diff
- Updates only the changed elements
Next Steps
Try adding more features: form inputs, keyboard shortcuts, or connect it to a database. Check out our documentation for more examples.
Related Posts
djust 0.2.2: The Debug Panel Gets Real
djust 0.2.2 transforms the debug panel from a static inspector into a live development companion. Event filtering, replay, network inspection, VDOM patch tracing, and live state updates via WebSocket — all wired up and working out of the box.
djust 0.2.1: WebSocket Security Hardening with Three Layers of Defense
djust 0.2.1 locks down WebSocket event dispatch with an event name guard, @event_handler decorator allowlist, server-side rate limiting, and per-IP connection tracking. A security-focused release with zero new features to break.
58 Ways to Break a VDOM (and Why Ours Didn't)
We wrote 58 torture tests targeting every weak spot in djust's virtual DOM diff engine: 50-level deep trees, 500-sibling lists, keyed shuffles, duplicate keys, and rapid-fire state updates. Everything passed. Here's what we tested, what we found, and why it matters.