Mixins¶
The django_htmx_plus.mixins module provides reusable mixins for HTMX-enhanced views.
HtmxFormResponseMixin¶
from django_htmx_plus.mixins import HtmxFormResponseMixin
A mixin for FormView and CreateView that replaces the default success redirect with an HTMX trigger response.
Attributes¶
- valid_triggers: List[str]¶
List of HTMX event names to include in the
HX-Triggerheader on successful form submission.Default:
[]
- success_message: str | None¶
Optional Django success message to queue after form submission. The message is automatically added to the
HX-Triggerheader byHtmxMessagesMiddleware.Default:
None
Example¶
from django.views.generic.edit import CreateView
from django_htmx_plus.mixins import HtmxFormResponseMixin
from myapp.models import Article
from myapp.forms import ArticleForm
class ArticleCreateView(HtmxFormResponseMixin, CreateView):
model = Article
form_class = ArticleForm
template_name = "articles/form.html"
valid_triggers = ["articleCreated", "refreshList"]
success_message = "Article created successfully."
Behavior¶
On successful form submission:
The form is saved (by calling
form.save())The optional success message is queued
An
HtmxResponsewith the specified triggers is returned (204 No Content)
On form validation errors:
The form is re-rendered with errors as usual
No HTMX response is sent
This allows you to build inline forms and modals that close/update automatically on success and display validation errors in place.