https://htmx.org logo
If it can help, I built a specific mixin to be use...
# 🔥-django-htmx
r
If it can help, I built a specific mixin to be used with form validation for create/update generic views. It looks like this:
Copy code
python
class HTMXCreateOrUpdateMixin(RefreshTableMixin, EmptyResponseMixin):
    """As create and update operations share almost the same logic, it makes sense
    to gather this logic into a mixin to avoid repetition.
    Note this is a mixin, and can not be used as is like a View.
    """

    form_class = None
    form_template_name = None
    hx_target_form_invalid = None

    def get(self, request, *args, **kwargs):
        check_not_none(self, "form_class", "form_template_name")
        context = self.get_context_data()
        return render(request, self.form_template_name, context)

    def post(self, request, *args, **kwargs):
        check_not_none(self, "form_class", "form_template_name", "hx_target_form_invalid")
        form = self.get_form()
        context = self.get_context_data(form=form)
        if form.is_valid():
            obj = form.save()
            response = self.build_response(request)
        else:
            response = render(request, self.form_template_name, context)
            response["HX-Reswap"] = "outerHTML"
            response["HX-Retarget"] = self.hx_target_form_invalid

        return response