From e65b2a9fb353f042ebaf3a46a3bdc13f2fd14751 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 31 Jan 2023 10:07:24 -0500 Subject: [PATCH] Closes #11625: Add HTMX support to ObjectEditView --- docs/release-notes/version-3.5.md | 5 +- netbox/netbox/views/generic/object_views.py | 6 +++ netbox/templates/generic/object_edit.html | 57 ++------------------- netbox/templates/htmx/form.html | 51 ++++++++++++++++++ 4 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 netbox/templates/htmx/form.html diff --git a/docs/release-notes/version-3.5.md b/docs/release-notes/version-3.5.md index 6d0ab183479..ae2d319b347 100644 --- a/docs/release-notes/version-3.5.md +++ b/docs/release-notes/version-3.5.md @@ -4,10 +4,11 @@ ### Enhancements -* [#11517](https://github.com/netbox-community/netbox/issues/11517) - Standardize the inclusion of related objects across the entire UI -* [#11584](https://github.com/netbox-community/netbox/issues/11584) - Add a list view for contact assignments * [#11254](https://github.com/netbox-community/netbox/issues/11254) - Introduce the `X-Request-ID` HTTP header to annotate the unique ID of each request for change logging * [#11440](https://github.com/netbox-community/netbox/issues/11440) - Add an `enabled` field for device type interfaces +* [#11517](https://github.com/netbox-community/netbox/issues/11517) - Standardize the inclusion of related objects across the entire UI +* [#11584](https://github.com/netbox-community/netbox/issues/11584) - Add a list view for contact assignments +* [#11625](https://github.com/netbox-community/netbox/issues/11625) - Add HTMX support to ObjectEditView ### Other Changes diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index 475cca9d30a..2dff8b27438 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -218,6 +218,12 @@ def get(self, request, *args, **kwargs): form = self.form(instance=obj, initial=initial_data) restrict_form_fields(form, request.user) + # If this is an HTMX request, return only the rendered form HTML + if is_htmx(request): + return render(request, 'htmx/form.html', { + 'form': form, + }) + return render(request, self.template_name, { 'model': model, 'object': obj, diff --git a/netbox/templates/generic/object_edit.html b/netbox/templates/generic/object_edit.html index c61fb723fe8..8531ad6dfad 100644 --- a/netbox/templates/generic/object_edit.html +++ b/netbox/templates/generic/object_edit.html @@ -1,6 +1,4 @@ {% extends 'base/layout.html' %} -{% load form_helpers %} -{% load helpers %} {% comment %} Blocks: @@ -48,56 +46,11 @@
{% csrf_token %} - {% block form %} - {% if form.fieldsets %} - - {# Render hidden fields #} - {% for field in form.hidden_fields %} - {{ field }} - {% endfor %} - - {# Render grouped fields according to Form #} - {% for group, fields in form.fieldsets %} -
- {% if group %} -
-
{{ group }}
-
- {% endif %} - {% for name in fields %} - {% with field=form|getfield:name %} - {% if not field.field.widget.is_hidden %} - {% render_field field %} - {% endif %} - {% endwith %} - {% endfor %} -
- {% endfor %} - - {% if form.custom_fields %} -
-
-
Custom Fields
-
- {% render_custom_fields form %} -
- {% endif %} - - {% if form.comments %} -
-
Comments
- {% render_field form.comments %} -
- {% endif %} - - {% else %} - {# Render all fields in a single group #} -
- {% render_form form %} -
- {% endif %} - - {% endblock form %} +
+ {% block form %} + {% include 'htmx/form.html' %} + {% endblock form %} +
{% block buttons %} diff --git a/netbox/templates/htmx/form.html b/netbox/templates/htmx/form.html new file mode 100644 index 00000000000..e5a2ab6c6c5 --- /dev/null +++ b/netbox/templates/htmx/form.html @@ -0,0 +1,51 @@ +{% load form_helpers %} + +{% if form.fieldsets %} + + {# Render hidden fields #} + {% for field in form.hidden_fields %} + {{ field }} + {% endfor %} + + {# Render grouped fields according to Form #} + {% for group, fields in form.fieldsets %} +
+ {% if group %} +
+
{{ group }}
+
+ {% endif %} + {% for name in fields %} + {% with field=form|getfield:name %} + {% if not field.field.widget.is_hidden %} + {% render_field field %} + {% endif %} + {% endwith %} + {% endfor %} +
+ {% endfor %} + + {% if form.custom_fields %} +
+
+
Custom Fields
+
+ {% render_custom_fields form %} +
+ {% endif %} + + {% if form.comments %} +
+
Comments
+ {% render_field form.comments %} +
+ {% endif %} + +{% else %} + + {# Render all fields in a single group #} +
+ {% render_form form %} +
+ +{% endif %}