From 91df0c0686bed7dd8303bcafef13a5aa073fedc6 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 22 Aug 2024 08:43:15 -0400 Subject: [PATCH] Closes #17221: Extend ObjectEditView to support HTMX requests --- netbox/netbox/views/generic/object_views.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index 85f90cbc125..35726452205 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -234,6 +234,8 @@ def get(self, request, *args, **kwargs): # If this is an HTMX request, return only the rendered form HTML if htmx_partial(request): return render(request, self.htmx_template_name, { + 'model': model, + 'object': obj, 'form': form, }) @@ -288,6 +290,7 @@ def post(self, request, *args, **kwargs): msg = f'{msg} {obj}' messages.success(request, msg) + # If adding another object, redirect back to the edit form if '_addanother' in request.POST: redirect_url = request.path @@ -303,6 +306,12 @@ def post(self, request, *args, **kwargs): 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) except (AbortRequest, PermissionsViolation) as e: