Skip to content

Commit 147a4cb

Browse files
committed
Closes #7239: Redirect global search to filtered object list when an object type is selected
1 parent ab0a2ab commit 147a4cb

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

docs/release-notes/version-3.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v3.0.3 (FUTURE)
44

5+
### Enhancements
6+
7+
* [#7239](https://github.com/netbox-community/netbox/issues/7239) - Redirect global search to filtered object list when an object type is selected
8+
59
### Bug Fixes
610

711
* [#7167](https://github.com/netbox-community/netbox/issues/7167) - Ensure consistent font size when using monospace formatting

netbox/netbox/views/__init__.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from netbox.constants import SEARCH_MAX_RESULTS, SEARCH_TYPES
2727
from netbox.forms import SearchForm
2828
from tenancy.models import Tenant
29-
from utilities.tables import paginate_table
3029
from virtualization.models import Cluster, VirtualMachine
3130

3231

@@ -154,26 +153,18 @@ def build_stats():
154153
class SearchView(View):
155154

156155
def get(self, request):
157-
158-
# No query
159-
if 'q' not in request.GET:
160-
return render(request, 'search.html', {
161-
'form': SearchForm(),
162-
})
163-
164156
form = SearchForm(request.GET)
165157
results = []
166158

167159
if form.is_valid():
168160

161+
# If an object type has been specified, redirect to the dedicated view for it
169162
if form.cleaned_data['obj_type']:
170-
# Searching for a single type of object
171-
obj_types = [form.cleaned_data['obj_type']]
172-
else:
173-
# Searching all object types
174-
obj_types = SEARCH_TYPES.keys()
163+
object_type = form.cleaned_data['obj_type']
164+
url = reverse(SEARCH_TYPES[object_type]['url'])
165+
return redirect(f"{url}?q={form.cleaned_data['q']}")
175166

176-
for obj_type in obj_types:
167+
for obj_type in SEARCH_TYPES.keys():
177168

178169
queryset = SEARCH_TYPES[obj_type]['queryset'].restrict(request.user, 'view')
179170
filterset = SEARCH_TYPES[obj_type]['filterset']

netbox/templates/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ <h3 class="text-muted text-center">No results found</h3>
5454
{% endif %}
5555
{% else %}
5656
<div class="row">
57-
<div class="col col-12 col-lg-4 offset-lg-4">
57+
<div class="col col-12 col-lg-6 offset-lg-3">
5858
<form action="{% url 'search' %}" method="get" class="form form-horizontal">
5959
<div class="card">
6060
<h5 class="card-header">

0 commit comments

Comments
 (0)