Skip to content

feat: Add HTMX-powered search to snippet list (#510)#660

Open
Theminacious wants to merge 4 commits into
django:mainfrom
Theminacious:feature/htmx-table-search
Open

feat: Add HTMX-powered search to snippet list (#510)#660
Theminacious wants to merge 4 commits into
django:mainfrom
Theminacious:feature/htmx-table-search

Conversation

@Theminacious
Copy link
Copy Markdown

Overview

Fixes #510

This PR integrates the search functionality directly into the snippet list page using HTMX. This allows users to filter snippets in real-time without a full page reload, addressing the issue's request to eliminate redundant navigation to the separate search page.

Changes

  • Frontend:
    • moved the search bar to the snippet list header for better accessibility.
    • extracted the snippet table into a reusable partial template: cab/partials/_snippet_table.html.
    • added HTMX attributes (hx-get, hx-target, hx-swap, hx-push-url) to the search input.
  • Backend:
    • updated snippet_list view to handle search logic directly (PostgreSQL Full-Text Search with icontains fallback).
    • added logic to detect request.htmx and render only the table partial.
  • Configuration:
    • added django-htmx to INSTALLED_APPS and MIDDLEWARE.

Key Features

  • Real-time Filtering: The table updates instantly as the user types.
  • 🔗 Shareable URLs: The browser URL updates automatically (e.g., ?q=django) so searches can be bookmarked.
  • 📱 Responsive Design: The search bar works on both mobile and desktop layouts.
  • 🛡️ Graceful Degradation: Standard page loads still work perfectly if JS is disabled.

Testing

  • Verified that standard (non-HTMX) requests render the full page.
  • Verified that HTMX requests return only the table HTML fragment.
  • Tested search queries with and without results.

- Add search input to snippet list header for seamless filtering
- Implement HTMX partial updates (no full page reload)
- Use PostgreSQL full-text search with ranking for relevance
- Include icontains fallback for compatibility
- Preserve search queries in URL for sharing and bookmarking
- Create reusable partial template for snippet table
- Responsive design (mobile-first, desktop-optimized)

This reduces the need to navigate to the separate /search/ page
for common queries while maintaining backward compatibility.
Copilot AI review requested due to automatic review settings February 10, 2026 12:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Integrates HTMX-driven searching directly into the snippet list page so filtering updates the snippet table without a full page reload, addressing issue #510.

Changes:

  • Adds an HTMX-enabled search input to the snippet list header and renders results via partial swapping.
  • Extracts the snippet table + pagination into a reusable partial template.
  • Updates the snippet_list view to apply search filtering and return the partial template for HTMX requests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
djangosnippets/templates/cab/snippet_list.html Adds the HTMX search UI in the list header and includes the new table partial in the page body.
djangosnippets/templates/cab/partials/_snippet_table.html New partial for rendering the snippet table + pagination (or empty state) as an HTMX swap target.
cab/views/snippets.py Implements query filtering (Postgres FTS with fallback) and switches to partial rendering for HTMX requests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cab/views/snippets.py Outdated
Comment thread cab/views/snippets.py Outdated
Comment thread cab/views/snippets.py
Comment thread djangosnippets/templates/cab/snippet_list.html
Comment thread djangosnippets/templates/cab/snippet_list.html Outdated
Comment thread djangosnippets/templates/cab/snippet_list.html
Comment thread djangosnippets/templates/cab/snippet_list.html Outdated
Comment thread djangosnippets/templates/cab/snippet_list.html
- Replace blanket Exception with specific DB exceptions (NotSupportedError, ProgrammingError)
  for better error visibility and debugging
- Increase MIN_QUERY_LENGTH from 2 to 3 to prevent expensive DB queries on very short inputs
- Improve HTMX trigger: use 'input' event with 500ms delay for better paste/autocomplete support
- Add method='get' to form for graceful degradation when HTMX fails
- Remove onsubmit='return false;' to allow normal form submission fallback
- Preserve existing query parameters (sorting, filters) when searching via hidden inputs
- Add sr-only label for accessibility (screen reader support)
- Add comprehensive test suite covering HTMX, search filtering, and edge cases
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cab/tests/tests.py Outdated
Comment thread cab/views/snippets.py Outdated
Comment thread cab/views/snippets.py Outdated
Comment thread cab/views/snippets.py Outdated
Comment thread djangosnippets/templates/cab/snippet_list.html
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cab/views/snippets.py Outdated
@Theminacious
Copy link
Copy Markdown
Author

All review feedback has been addressed and outdated suggestions resolved.
Please let me know if any additional adjustments are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improving table search using partial_template and django_htmx.

2 participants