Closes #17221: Extend ObjectEditView to support HTMX requests

This commit is contained in:
Jeremy Stretch 2024-08-22 08:43:15 -04:00
parent fb6c7d7619
commit 91df0c0686

View File

@ -234,6 +234,8 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
# If this is an HTMX request, return only the rendered form HTML # If this is an HTMX request, return only the rendered form HTML
if htmx_partial(request): if htmx_partial(request):
return render(request, self.htmx_template_name, { return render(request, self.htmx_template_name, {
'model': model,
'object': obj,
'form': form, 'form': form,
}) })
@ -288,6 +290,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
msg = f'{msg} {obj}' msg = f'{msg} {obj}'
messages.success(request, msg) messages.success(request, msg)
# If adding another object, redirect back to the edit form
if '_addanother' in request.POST: if '_addanother' in request.POST:
redirect_url = request.path redirect_url = request.path
@ -303,6 +306,12 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
return_url = self.get_return_url(request, obj) return_url = self.get_return_url(request, obj)
# If the object has been created or edited via HTMX, return an HTMX redirect to the object view
if request.htmx:
return HttpResponse(headers={
'HX-Location': return_url,
})
return redirect(return_url) return redirect(return_url)
except (AbortRequest, PermissionsViolation) as e: except (AbortRequest, PermissionsViolation) as e: