feat: implement article search with PostgreSQL full-text search
- Configure Wagtail database search backend with English search config - Add django.contrib.postgres to INSTALLED_APPS for full PG FTS support - Expand ArticlePage.search_fields: body_text (excl. code blocks), AutocompleteField(title), RelatedFields(tags), FilterFields - Add search view at /search/?q= with query guards (strip, max 200 chars, empty/whitespace handling) and pagination preserving query param - Replace nav Subscribe CTA with compact search box (desktop + mobile) - Add search box to article index page alongside category/tag filters - Create search results template reusing article_card component - Add update_index to deploy entrypoint for automated reindexing - Update existing tests for nav change, add comprehensive search tests Closes #41 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
43
apps/blog/views.py
Normal file
43
apps/blog/views.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
from apps.blog.models import ArticlePage
|
||||
|
||||
RESULTS_PER_PAGE = 12
|
||||
MAX_QUERY_LENGTH = 200
|
||||
|
||||
|
||||
def search(request: HttpRequest) -> HttpResponse:
|
||||
query = request.GET.get("q", "").strip()[:MAX_QUERY_LENGTH]
|
||||
results_page = None
|
||||
paginator = None
|
||||
|
||||
if query:
|
||||
results = (
|
||||
ArticlePage.objects.live()
|
||||
.public()
|
||||
.select_related("author", "category")
|
||||
.prefetch_related("tags__metadata")
|
||||
.search(query)
|
||||
)
|
||||
paginator = Paginator(results, RESULTS_PER_PAGE)
|
||||
page_num = request.GET.get("page")
|
||||
try:
|
||||
results_page = paginator.page(page_num)
|
||||
except PageNotAnInteger:
|
||||
results_page = paginator.page(1)
|
||||
except EmptyPage:
|
||||
results_page = paginator.page(paginator.num_pages)
|
||||
|
||||
return TemplateResponse(
|
||||
request,
|
||||
"blog/search_results.html",
|
||||
{
|
||||
"query": query,
|
||||
"results": results_page,
|
||||
"paginator": paginator,
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user