Поисковая строка и вывод товаров по нему Django
я новичок в Django, делаю сайт доску объявлений. Появилась проблема с поиском. При вводе названия товара в поиск и нажатии enter ничего не выводит.
models.py
class Products(models.Model):
title = models.TextField()
description = models.TextField()
price = models.TextField()
images = models.TextField()
views.py
from .models import Products
from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank
from django.views.generic import ListView
class ProductsSearchResultView(ListView):
model = Products
context_object_name = 'articles'
paginate_by = 10
allow_empty = True
template_name = 'index.html'
def get_queryset(self):
query = self.request.GET.get('do')
search_vector = SearchVector('title', weight='B') + SearchVector('description', weight='A')
search_query = SearchQuery(query)
return (self.model.objects.annotate(rank=SearchRank(search_vector, search_query)).filter(rank__gte=0.3).order_by('-rank'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = f'Результаты поиска: {self.request.GET.get("do")}'
return context
def index(request):
prods = Products.objects.all()
return render(request, 'index.html', context = {
'prods' : prods
})
urls.py
from test_app import views
from test_app.views import ProductsSearchResultView
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name = 'index'),
path('index.html', views.index, name = 'index'),
path('search/', ProductsSearchResultView.as_view(), name='search'),
]
index.html
<form role="search" method="get" action="{% url 'search' %}">
<input type="search" placeholder="Поиск" aria-label="Search" name='do' autocomplete="off" id="search">
</form>