'How to change HTMX behavior to replace the full web page if form is invalid

If this form submits as invalid, I want to override the htmx and do a HttpRedirectResponse that refreshes the full page instead of just changing the div, #superdiv, contents.

<form id="create_form" action="." method="post" hx-post="." hx-target="#superdiv" hx swap="outerHTML">
        {{ form }}
        <button id="button" type="submit">Create</button>
    </form>

I have been attempting to do this in the def post of my views.

if form.is_valid():
    return self.form_valid(form)
else:
    return HttpResponseRedirect(reverse_lazy("logs:manager_form"))

Would this be achieved better using JavaScript? I am trying my best to sticking to learning only vanilla JavaScript for now.

Thanks for any help.



Solution 1:[1]

I don't know if you already use it but the django-htmx extension is really good and handy.

You can trigger an entire client refresh from your Django view.

HttpResponseClientRefresh

I hope it is useful ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luca Fedrizzi