Add autocomplete functionality for parks: implement URLs, views, and templates for real-time suggestions

This commit is contained in:
pacnpal
2025-02-23 13:07:27 -05:00
parent 8e9b6b6a15
commit 1876af46d9
26 changed files with 862 additions and 191 deletions

View File

@@ -0,0 +1,61 @@
# Search Duplication Fix
## Issue
The park search was showing duplicate results because:
1. There were two separate forms with the same ID ("filter-form")
2. Both forms were targeting the same element ("#park-results")
3. The search form and filter form were operating independently
## Solution
1. Created a custom autocomplete package to handle search functionality:
- ModelAutocomplete base class for model-based autocomplete
- AutocompleteWidget for rendering the search input
- Templates for widget and suggestions
- Views for handling search and selection
2. Updated ParkAutocomplete to use ModelAutocomplete:
```python
class ParkAutocomplete(ModelAutocomplete):
model = Park
search_attrs = ['name']
minimum_search_length = 2
max_results = 8
```
3. Combined search and filter functionality into a single form:
```html
<form id="filter-form"
x-ref="filterForm"
hx-get="{% url 'parks:park_list' %}"
hx-target="#park-results"
hx-push-url="true"
hx-trigger="submit"
class="mt-4">
<div class="mb-6">
{{ search_form }} <!-- AutocompleteWidget -->
</div>
{% include "search/components/filter_form.html" with filter=filter %}
</form>
```
4. Added proper URL routing for autocomplete:
```python
path("ac/", include((autocomplete_patterns, "autocomplete"), namespace="autocomplete"))
```
## Benefits
1. No more duplicate search requests
2. Cleaner template structure
3. Better user experience with a single search interface
4. Proper integration with django-htmx-autocomplete
5. Simplified view logic
6. Reusable autocomplete functionality for other models
## Technical Details
- Using django-htmx-autocomplete's AutocompleteWidget for search
- Single form submission handles both search and filtering
- HTMX handles the dynamic updates
- View mode selection preserved during search/filter operations
- Minimum search length of 2 characters
- Maximum of 8 search results
- Search results include park status and location