alpinejs-django
Use when adding, changing, or debugging Alpine.js behavior in this Django SaaS Starter project, especially in Django templates that may also use HTMX partial updates.
- License 许可
- MIT
- 最近更新
- 2026-06-26
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- alpinejs-django
- license
- MIT
- metadata
- { "tags": "alpinejs,django,frontend,htmx", "version": "0.1.0", "category": "Django", "displayName": "Alpine.js Django" }
- description
- Use when adding, changing, or debugging Alpine.js behavior in this Django SaaS Starter project, especially in Django templates that may also use HTMX partial updates.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
- 资产文件assets/app-icon.png37.2 KB
03 SKILL.md 正文
Alpine.js with Django
Use this skill for local browser state in Django-rendered templates: dropdowns, modals, tabs, disclosures, inline filters, disabled/loading states, small preview interactions, and client-only toggles. Use Django views/forms/models as the source of truth for durable state. Use HTMX when the server needs to return fresh HTML.
Project setup
- Alpine is installed from npm and copied by
scripts/copy-vendor-assets.mjstofrontend/static/vendors/js/alpine.min.js. base_app.htmlandbase_landing.htmlload HTMX first, Alpine second, andfrontend/static/js/app.jsthird.- The loaded Alpine file is the self-starting CDN build. Do not call
Alpine.start()fromfrontend/src/js/app.js. - Simple Alpine behavior should usually live inline in the Django template with
x-data. - If a reusable component or store is worth extracting, register it before
Alpine starts. With the current script order,
frontend/src/js/app.jsruns too late for reliabledocument.addEventListener("alpine:init", ...)registration unless the script order is changed. - When reusable Alpine registration is needed, add a dedicated classic script
such as
frontend/src/js/alpine-components.js, register components/stores insidedocument.addEventListener("alpine:init", ...), and include the copiedfrontend/static/js/alpine-components.jsbeforevendors/js/alpine.min.jsin both base templates. Keepapp.jsafter Alpine, keep the self-starting Alpine build, and still do not callAlpine.start()manually.
Ownership rules
- Django owns persistence, authorization, validation, redirects, and rendered HTML.
- HTMX owns server round trips and DOM swaps.
- Alpine owns ephemeral state already present in the browser.
- Plain modules in
frontend/src/js/own shared DOM behavior that is not naturally scoped to one Alpine component. - Do not duplicate the same behavior in Alpine and a plain JS module.
Template patterns
Use a small x-data object near the markup it controls:
<div
x-data="{ open: false }"
@keydown.escape.window="open = false"
@click.outside="open = false"
>
<button
type="button"
:aria-expanded="open.toString()"
@click.stop="open = !open"
>
Menu
</button>
<div x-cloak x-show="open" x-transition>
...
</div>
</div>
Prefer Alpine directives over manual DOM manipulation:
x-showfor toggling visibility while keeping the element in the DOM.x-ifon a<template>when the element should be created and destroyed.x-modelfor client-only input state; Django form submission and validation still happen on the server.:class,:disabled,:aria-expanded, and:hiddenfor state-derived attributes.$watchfor one named state transition;x-effectonly when the dependency set is simple and intentional.$dispatchfor browser events between Alpine components or from Alpine to HTMX triggers.
Use x-cloak for anything hidden by default. The base templates already include
the required [x-cloak] { display: none !important; } CSS.
Django data
Keep Django interpolation out of complex JavaScript expressions when possible.
For simple values, prefer HTML attributes:
<div
data-initial-label="{{ object.name }}"
x-data="{ label: '' }"
x-init="label = $el.dataset.initialLabel"
>
<span x-text="label"></span>
</div>
For structured values, prefer Django's json_script and parse it in x-init:
{{ rows|json_script:"rows-data" }}
<div
x-data="{ rows: [] }"
x-init="rows = JSON.parse(document.getElementById('rows-data').textContent)"
>
<template x-for="row in rows" :key="row.id">
<span x-text="row.name"></span>
</template>
</div>
Avoid |safe inside Alpine expressions. If the browser must display text, use
x-text. Use x-html only for trusted, already-sanitized HTML; most HTML should
be rendered by Django or returned through HTMX.
Alpine and HTMX
When HTMX swaps a fragment containing x-data, Alpine should initialize the new
component automatically. Do not restart Alpine after HTMX swaps.
Keep persistent Alpine state outside HTMX targets that will be replaced:
<section x-data="{ panelOpen: false }">
<button type="button" @click="panelOpen = !panelOpen">Filters</button>
<div id="results" hx-get="{% url 'search_results' %}" hx-trigger="change from:#filters">
...
</div>
</section>
Use events as the boundary between HTMX responses and Alpine state. From Django,
set an HX-Trigger or HX-Trigger-After-Swap response header, then listen from
Alpine:
<div x-data="{ open: true }" @profile-saved.window="open = false">
<form hx-post="{% url 'profile_update' %}" hx-target="#profile-panel" hx-swap="outerHTML">
...
</form>
</div>
Listen for HTMX lifecycle events in kebab case from Alpine, such as
@htmx:after-swap.window, because HTML attributes are case-insensitive.
If Alpine dispatches an event that HTMX should react to, make the trigger explicit:
<button type="button" x-data @click="$dispatch('refresh-results')">
Refresh
</button>
<div hx-get="{% url 'results' %}" hx-trigger="refresh-results from:body">
...
</div>
Accessibility
- Keep real buttons as
<button type="button">unless submitting a form. - Bind ARIA state from Alpine state, especially
aria-expanded,aria-controls,aria-selected, andaria-hidden. - Support
@keydown.escape.windowfor dismissible overlays. - Use
@click.outsidefor popovers and menus, paired with a visible trigger. - Preserve focus behavior for modals, menus, and swapped HTMX content. If focus trapping is needed, add a focused plain JS module or Alpine plugin deliberately instead of hand-rolling a brittle trap in attributes.
Security and CSP
- Treat all Alpine expressions as JavaScript running in the user's browser.
- Do not put secrets, signed tokens, or privileged data into
x-data,data-*,json_script, or hidden inputs unless the user is allowed to see them. - If strict Content Security Policy without
unsafe-evalis required, switch deliberately to Alpine's CSP build and retest Alpine expressions. The defaultalpinejs/dist/cdn.min.jsbuild is not the CSP build. - If Alpine code makes
fetch()requests, include Django CSRF headers or use existing HTMX/Django forms instead.
Validation
- For template-only Alpine changes, run the Django template or view tests that
cover the page, plus
npm run lintif JavaScript modules changed. - For HTMX interactions, test the full page and the partial response path.
- For generated projects, run
npm run buildbefore relying on static output. - Manually verify stateful controls in light and dark mode when changing visible UI behavior.
04 同仓库更多 Skills
android-emulator-qa
Use when validating Android feature flows in an emulator with adb-driven launch, input, UI-tree inspection, screenshots, and logcat capture.
supabase-postgres-best-practices
Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.
sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
render-workflows
Sets up, develops, tests, and deploys Render Workflows. Covers first-time scaffolding (via CLI or manual), SDK installation (Python or TypeScript), task patterns (retries, subtasks, fan-out), local development, Dashboard deployment, and troubleshooting. Use when a user wants to set up Render Workflows for the first time, scaffold a workflow service, add or modify workflow tasks, test workflows locally, or deploy workflows to Render.
render-web-services
Configures Render web services—port binding, TLS, health checks, custom domains, auto-deploy, PR previews, persistent disks, and deploy lifecycle. Use when the user needs to set up a web service, fix health check failures, add a custom domain, configure zero-downtime deploys, or troubleshoot port binding issues.
render-static-sites
Deploys and configures static sites on Render's global CDN—build commands, publish paths, SPA routing, redirects, custom headers, and PR previews. Use when the user needs to deploy a static site, set up a React/Vue/Hugo/Gatsby frontend, configure SPA fallback routing, add redirect rules, customize response headers, or choose between a static site and a web service for their frontend. Trigger terms: static site, CDN, SPA, single-page app, React deploy, Vue deploy, Hugo, Gatsby, Docusaurus, Jekyll, staticPublishPath.