Я делаю поиск по полю name на своём сайте, однако когда я нажимаю на кнопку ПОИСК, результатов поиска не вижу.
Views.py(только нужные классы)
class Index(ListView):
model = Product
template_name = 'magazin/index.html'
context_object_name = 'page'
queryset = (Product
.objects.select_related('category')
.filter(is_on_main=True)
.values('pk','name','price','ir'))
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['cat'] = Category.objects.all()
return ctx
class Search(ListView):
def get_queryset(self):
return Product.objects.filter(name__icontains=self.request.GET.get('q'))
def get_context_data(self, *args,**kwargs):
ctx = super().get_context_data(*args,**kwargs)
ctx['q'] = self.request.GET.get('q')
return ctx
models.py(Product)
class Product(models.Model):
is_on_main = models.BooleanField(default=False)
category = models.ForeignKey(Category, on_delete = models.CASCADE)
name = models.CharField(max_length=50)
price = models.IntegerField()
date = models.DateTimeField(null = True,auto_now=True)
url = models.SlugField(max_length=100,unique=True,null=True)
description = models.TextField(max_length=2000,default='Описание')
ir = models.DateTimeField(default=(timezone.now() - datetime.timedelta(minutes=30)))
def __str__(self):
return self.name
class Meta:
ordering = '-date',
index.html(форма поиска)
<form action="{% url 'search' %}" method="get">
<input class="g" type="search" placeholder="Я ищу...",name='q'>
<button type="submit">ПОИСК</button>
</form>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.Index.as_view(),name='index'),
path('<slug:url>/',views.cat,name='vc'),
path('<int:pk>', views.Detail.as_view(),name='detail'),
path('add',views.add,name='add'),
path('search/',views.Search.as_view(),name='search')
]