Skip to content

Commit e12da72

Browse files
committed
Fixes #8101: Preserve return URL when using "create and add another" button
1 parent f95e510 commit e12da72

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

docs/release-notes/version-3.1.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [#8088](https://github.com/netbox-community/netbox/issues/8088) - Improve legibility of text in labels with light-colored backgrounds
2323
* [#8092](https://github.com/netbox-community/netbox/issues/8092) - Rack elevations should not include device asset tags
2424
* [#8096](https://github.com/netbox-community/netbox/issues/8096) - Fix DataError during change logging of objects with very long string representations
25+
* [#8101](https://github.com/netbox-community/netbox/issues/8101) - Preserve return URL when using "create and add another" button
2526
* [#8102](https://github.com/netbox-community/netbox/issues/8102) - Raise validation error when attempting to assign an IP address to multiple objects
2627

2728
---

netbox/netbox/views/generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,11 @@ def post(self, request, *args, **kwargs):
371371
redirect_url = request.path
372372

373373
# If the object has clone_fields, pre-populate a new instance of the form
374-
if hasattr(obj, 'clone_fields'):
375-
redirect_url += f"?{prepare_cloned_fields(obj)}"
374+
params = prepare_cloned_fields(obj)
375+
if 'return_url' in request.GET:
376+
params['return_url'] = request.GET.get('return_url')
377+
if params:
378+
redirect_url += f"?{params.urlencode()}"
376379

377380
return redirect(redirect_url)
378381

netbox/utilities/templatetags/buttons.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def clone_button(instance):
3030
url = reverse(_get_viewname(instance, 'add'))
3131

3232
# Populate cloned field values
33-
param_string = prepare_cloned_fields(instance)
33+
param_string = prepare_cloned_fields(instance).urlencode()
3434
if param_string:
3535
url = f'{url}?{param_string}'
3636

netbox/utilities/utils.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.core.serializers import serialize
99
from django.db.models import Count, OuterRef, Subquery
1010
from django.db.models.functions import Coalesce
11+
from django.http import QueryDict
1112
from jinja2.sandbox import SandboxedEnvironment
1213
from mptt.models import MPTTModel
1314

@@ -249,10 +250,8 @@ def prepare_cloned_fields(instance):
249250
for tag in instance.tags.all():
250251
params.append(('tags', tag.pk))
251252

252-
# Concatenate parameters into a URL query string
253-
param_string = '&'.join([f'{k}={v}' for k, v in params])
254-
255-
return param_string
253+
# Return a QueryDict with the parameters
254+
return QueryDict('&'.join([f'{k}={v}' for k, v in params]), mutable=True)
256255

257256

258257
def shallow_compare_dict(source_dict, destination_dict, exclude=None):

0 commit comments

Comments
 (0)