Skip to content

Commit

Permalink
Closes #17221: Extend ObjectEditView to support HTMX requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Aug 23, 2024
1 parent fb6c7d7 commit 91df0c0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions netbox/netbox/views/generic/object_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit 91df0c0

Please sign in to comment.