From 4d145ebabee3f98a4735c6f5e3ea249ee748887e Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Fri, 21 Feb 2025 20:36:12 -0500 Subject: [PATCH] Implement park search suggestions: add autocomplete functionality and improve search input handling --- parks/templates/parks/park_list.html | 56 +++++++++++++++---- .../parks/partials/search_suggestions.html | 29 ++++++++++ parks/urls.py | 4 +- parks/views_search.py | 32 +++++++++++ 4 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 parks/templates/parks/partials/search_suggestions.html create mode 100644 parks/views_search.py diff --git a/parks/templates/parks/park_list.html b/parks/templates/parks/park_list.html index e8a3084f..73f06b3e 100644 --- a/parks/templates/parks/park_list.html +++ b/parks/templates/parks/park_list.html @@ -48,18 +48,43 @@
- +
+
+ +
+
+
+
{% endblock %} +{% block extra_css %} + +{% endblock %} + {% block extra_js %} + {% endblock %} \ No newline at end of file diff --git a/parks/templates/parks/partials/search_suggestions.html b/parks/templates/parks/partials/search_suggestions.html new file mode 100644 index 00000000..ee9f7cb8 --- /dev/null +++ b/parks/templates/parks/partials/search_suggestions.html @@ -0,0 +1,29 @@ +{% if suggestions %} + +{% endif %} \ No newline at end of file diff --git a/parks/urls.py b/parks/urls.py index aa6e18e6..3bf1ca5a 100644 --- a/parks/urls.py +++ b/parks/urls.py @@ -1,5 +1,5 @@ from django.urls import path, include -from . import views +from . import views, views_search from rides.views import ParkSingleCategoryListView app_name = "parks" @@ -18,6 +18,8 @@ urlpatterns = [ # Areas and search endpoints for HTMX path("areas/", views.get_park_areas, name="get_park_areas"), + path("suggestions/", views_search.suggest_parks, name="suggest_parks"), + path("search/", views.search_parks, name="search_parks"), # Park detail and related views diff --git a/parks/views_search.py b/parks/views_search.py new file mode 100644 index 00000000..e5123ea0 --- /dev/null +++ b/parks/views_search.py @@ -0,0 +1,32 @@ +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render +from .filters import ParkFilter +from .querysets import get_base_park_queryset + +def suggest_parks(request: HttpRequest) -> HttpResponse: + """Return park search suggestions as a dropdown""" + try: + query = request.GET.get('search', '').strip() + if not query or len(query) < 2: + return HttpResponse('') + + # Get current view mode from request + current_view_mode = request.GET.get('view_mode', 'grid') + park_filter = ParkFilter({ + 'search': query + }, queryset=get_base_park_queryset()) + + parks = park_filter.qs[:8] # Limit to 8 suggestions + + response = render( + request, + 'parks/partials/search_suggestions.html', + { + 'suggestions': parks, + 'view_mode': current_view_mode + } + ) + response['HX-Trigger'] = 'showSuggestions' + return response + except Exception as e: + return HttpResponse(f'Error getting suggestions: {str(e)}') \ No newline at end of file