How to search product in Django?

I am trying to create a search functionality in Django, I have some data store in my Django table and i am searching product name, but I am getting blank output, Please check my code and let me know where I am Mistaking.

Here is my urls.py file:

from .views import SearchResultsView  urlpatterns = [      path('search', SearchResultsView.as_view(), name='search_results'), ] 

here is my views.py file:

from django.views.generic import ListView from django.db.models import Q   class SearchResultsView(ListView):     model = Product     template_name = 'mainpage/search-product.html'     #prod=Product.objects.all()      def get_queryset(self):         query = self.request.GET.get('search')         products=Product.objects.filter(Q(name__icontains=query))         return products 

here is my base.html file:

<form action="/search" method="get">     {% csrf_token %}     <div class="form-group">         <input type="text" class="form-control" name="search" id="search exampleInputPassword1" placeholder="Search a Product">     </div>     <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button> </form> 

here is my models.py file:

class Product(models.Model):     name=models.CharField(max_length=225)     slug=models.SlugField(max_length=225, unique=True)     subcategory=models.ForeignKey('SubCategory', related_name='prosubcat', on_delete=models.CASCADE, blank=True, null=True)     totalprice=models.IntegerField()     saleprice = models.IntegerField()     discount = models.IntegerField(default=None)     title=models.CharField(max_length=225)     description = models.TextField()     overview = models.TextField(null=True)     featured = models.BooleanField(null=True)     image= models.ImageField(blank=True)     tags = TaggableManager()     created_at = models.DateTimeField(auto_now_add=True)     updated_at = models.DateTimeField(auto_now=True) 

here is my search-product.html file:

<div class="row margin-res">     {% for catproduct in products %}     <div class="col-xl-2 col-6 col-grid-box">         <div class="product-box">             <div class="img-wrapper">                 <div class="front">                     <a href="/product/{{catproduct.slug}}" class="bg-size blur-up lazyload" style="background-image: url(&quot;../assets/images/pro3/1.jpg&quot;); background-size: cover; background-position: center center; display: block;"><img src="/media/{{catproduct.image}}" class="img-fluid blur-up lazyload bg-img" alt="" style="display: none;"></a>                 </div>                 <div class="back">                     <a href="/product/{{catproduct.slug}}" class="bg-size blur-up lazyload" style="background-image: url(&quot;../assets/images/pro3/2.jpg&quot;); background-size: cover; background-position: center center; display: block;"><img src="/media/{{catproduct.image}}" class="img-fluid blur-up lazyload bg-img" alt="" style="display: none;"></a>                 </div>                 <div class="cart-info cart-wrap">                     <button data-toggle="modal" data-target="#addtocart" title="Add to cart"><i class="ti-shopping-cart"></i></button> <a href="javascript:void(0)" title="Add to Wishlist"><i class="ti-heart" aria-hidden="true"></i></a> <a href="#" data-toggle="modal" data-target="#quick-view" title="Quick View"><i class="ti-search" aria-hidden="true"></i></a> <a href="compare.html" title="Compare"><i class="ti-reload" aria-hidden="true"></i></a>                 </div>             </div>             <div class="product-detail">                 <div class="rating"><i class="fa fa-star"></i> <i class="fa fa-star"></i> <i class="fa fa-star"></i>                     <i class="fa fa-star"></i> <i class="fa fa-star"></i>                 </div>                 <a href="/product/{{catproduct.slug}}">                     <h6>{{catproduct.name}}</h6>                 </a>                 <p>{{catproduct.overview}}                 </p>                 <h4>₹ {{catproduct.saleprice}}</h4>             </div>         </div>     </div>     {% endfor %} </div> 
Add Comment
1 Answer(s)

Basically you are missing an attribute in Class named context_object_name which should have been set to products. Try like this:

class SearchResultsView(ListView):     model = Product     template_name = 'mainpage/search-product.html'     context_object_name = 'products'      def get_queryset(self):         query = self.request.GET.get('search')         products=Product.objects.filter(Q(name__icontains=query))         return products 

If you do not set this value, then the context_object which contains the queryset in template is named <model name>_list. More details can be found on how this is defined can be found here.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.