Home > Mobile >  Django search bar isn't giving correct results
Django search bar isn't giving correct results

Time:12-01

views.py

from django.shortcuts import render
from ecommerceapp.models import Product
from django.db.models import Q

def searchResult(request):
    products=None
    query=None
    if 'q' in request.GET:
        query = request.GET.get('q')
        products=Product.objects.all().filter(Q(name__contains=query) | Q(desc__contains=query))
    return render(request,'search.html',{'query':query,'products':products})

In views.py I have imported a model named 'Product' of another application.

search.html

{% extends 'base.html' %}
{% load static %}

{% block metadescription %}
    Welcome to FASHION STORE-Your Beauty
{% endblock %}

{% block title %}
   Search-FASHION STORE
{% endblock %}

{% block content %}
<div>
    <p >You have searched for :<b>"{{query}}"</b></p>
</div>
<div >
    <div >
        {% for product in products %}
            <div  >
                <div  style="min-width:18rem;">
                    <a href="{{product.get_url1}}"><img  src="{{product.image.url}}" alt="{{product.name}}" style="height:400px; width:100%;"></a>
                    <div >
                        <h4>{{product.name}}</h4>
                        <p>₹{{product.price}}</p>
                    </div>
                </div>
            </div>
            {% empty %}
            <div >
                <p >0 results found.</p>
            </div>
            {% endfor %}
    </div>
</div>
{% endblock %}

navbar.html

<nav >
  <div >
    <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span ></span>
    </button>
    <div  id="navbarSupportedContent">
      <ul >
        <li >
          <a  href="#">Home</a>
        </li>
        <li >
          <a  href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Shop
          </a>
          <ul >
            <li><a  href="{% url 'ecommerceapp:allProductCategory' %}">All Products</a></li>
            {% for cat in links %}
                <li><a  href="{{cat.get_url}}">{{cat.name}}</a></li>
            {% endfor %}
          </ul>
        </li>
        <li >
          <a  href=""><i ></i></a>
        </li>
      </ul>
      <form  action="{% url 'search_app:searchResult' %}" method="get">
        {% csrf_token %}
        <input  type="search" placeholder="Search" name="q" aria-label="Search">
        <button  type="submit"><i ></i></button>
      </form>
    </div>
  </div>
</nav>

When I'm searching using search bar, not getting the correct results. When giving the word completely, correct results are getting.

Example: When I type x in the search bar, it give me the results 'shirt' instead of giving '0 results found'.

CodePudding user response:

Example: When I type x in the search bar, it give me the results 'shirt' instead of giving '0 results found'.

The __contains is used to check whether the field contains given word or not, it is case-sensitive. And using | in Q objects means it is optional and works as OR condition, so maybe it is possible when you type x, the name field doesn't contains x but the field desc contain x that's why you are getting the shirt as instance or else you can simply put the query to Product.objects.filter(Q(name__contains=query)) and .all() only creates the copy of the Queryset so it doesn't require here.

  • Related