Copy-paste recipes for common HyperDjango interaction patterns.
The cookbook shows practical composition of features in real flows, not just isolated API references.
<section x-data="{ count: 0 }">
<button @click="$action('increment', { current: count })">
Increment
</button>
<strong x-text="count"></strong>
</section>from __future__ import annotations
from hyperdjango.integrations.alpine.actions import Signal
@action
def increment(self, request, current: int = 0, **params):
return [Signal(name="count", value=int(current) + 1)]from __future__ import annotations
from hyperdjango.integrations.alpine.actions import Signals
@action
def increment(self, request, current: int = 0, **params):
value = int(current) + 1
return [Signals(values={"count": value, "$count": value})]<section x-data="{ count: 0 }">
<button @click="$action('increment', { current: count })">Increment</button>
<p>Local: <span x-text="count"></span></p>
<p>Global: <span x-text="$store.hyper.count"></span></p>
</section>from __future__ import annotations
from hyperdjango.actions import HTML, action
@action
def show_editor(self, request, **params):
return [
HTML(
content=self.render(request=request, relative_template_name="partials/editor_form.html"),
target="#editor",
swap="inner",
)
]Client call can omit target and use server contract.
from __future__ import annotations
from hyperdjango.actions import HTML, Toast, action
@action
def add_todo(self, request, title="", **params):
return [
HTML(content="<li>...</li>", target="#todo-list", swap="append"),
HTML(content="<div>Updated stats</div>", target="#todo-stats"),
Toast(payload={"type": "success", "message": "Added"}),
]from __future__ import annotations
from hyperdjango.actions import HTML
return [
HTML(content="A", target="#one"),
HTML(content="<div id='two'>B</div>", target="#two", swap="outer"),
]from hyperdjango.actions import Delete
return [Delete(target=f"#todo-{id}")]<input
x-model="q"
@input.debounce.250ms="$action('search', { q }, { sync: 'replace', key: 'live-search' })"
/>
New requests abort in-flight ones in the same key.
<form id="profile-form"
x-data="{}"
@submit.prevent="$action('save_profile', {}, { form: $el, sync: 'block', key: 'profile-save' })">
...
</form>New submits while pending are ignored (blocked).
<div hyper-loading="live-search" hyper-loading-delay="150">Searching...</div>
<button hyper-loading-disable="live-search">Search</button>from __future__ import annotations
from hyperdjango.actions import HTML
if not form.is_valid():
return [
HTML(
content=self.render(
request=request,
relative_template_name="partials/form.html",
context_updates={"form": form},
),
target="#profile-panel",
focus="first-invalid",
)
]from __future__ import annotations
from hyperdjango.actions import History, HTML
return [
HTML(content="...", target="#results"),
History(replace_url=f"/search?q={query}"),
]Use push_url when you want a new history entry instead.
from __future__ import annotations
from hyperdjango.page import HyperPageTemplate
class ProfileCardTemplate(HyperPageTemplate):
passfrom __future__ import annotations
from hyperdjango.shortcuts import render_template_page
def profile_card(request):
return render_template_page(
request,
ProfileCardTemplate,
context={"title": "Account", "description": "From custom view"},
)