| With Django and flask and everything else I've ever used, there is a templating system at play. I use the request headers to determine if I should return a whole page or a portion of a page. For example I have a form that's something like form_partial.html: partial form template:
<form hx-post="{{ request.path }}" hx-swap="outerHTML" hx-encoding='multipart/form-data' hx-target="this">
{% csrftoken %}
... some other form stuff from django
</form> form.html: A normal page that includes that form:
{% extends core.html %}
{% include partial_form.html %} Then in the controller code I choose whether to render the entire page (form.html) or a partial page (partial_form.html) depending on the presence of the htmx headers. So the full load might look something like: 1. Visit mysite/coolform
2. Server renders whole page
3. Browser submits form data
4. If the form is invalid, return only the partial form, swapping the forms html
and if the form IS valid, return a redirect Another cool thing (checkout hx-boost ) about htmx is that even if you return a whole page you can actually still avoid a full page reload and only swap out the body component. So even though you're returning a few extra bytes the site isn't re-executing the entire JavaScript bundle every time you navigate to a new page. |