Home > Software design >  Django/PostgreSQL not displaying search result
Django/PostgreSQL not displaying search result

Time:07-09

I am unable to retrieve any search results when searching for items. I am using Postgresql and Django. Below is my code. I am not sure if I am not doing the search query right or I just cannot display the results.

I have the search bar in "inventory_management.html". I am trying to get it to where the user searches for an item, and then a list of the displayed items are shown.

Any help would be greatly appreciated!

models.py

class Inventory(models.Model):
    product = models.CharField(max_length=50)
    description = models.CharField(max_length=250)
    paid = models.DecimalField(null=True, max_digits=5, decimal_places=2)
    bin = models.CharField(max_length=4)
    listdate = models.DateField(null=True, blank=True)
    listprice = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
    solddate = models.DateField(null=True, blank=True)
    soldprice = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
    shipdate = models.DateField(null=True, blank=True)
    shipcost = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return self.product   "\n"   self.description   "\n"   self.paid   self.bin   "\n"   self.listdate   "\n"   self.listprice   "\n"   self.solddate   "\n"   self.soldprice   "\n"   self.shipdate   "\n"   self.shipcost

views.py

@login_required(login_url="/login")
def search(request):
    q = request.GET.get('q')

    if q:
        vector = SearchVector('product', 'description')
        query = SearchQuery(q)

        searchinv = Inventory.objects.annotate(search=vector).filter(search=query)
    else:
        searchinv = None

    return render(request, 'portal/search.html', {"searchinv": searchinv})

inventory_management.html (where the search bar is located)

{% extends 'portal/base.html' %}
{% block title %}{% endblock %}
{% block content %}
        <br>
        <div >
          <div >
           <form  role="search" action="/search" method="get">
              <input  type="text" name="q" placeholder="Search" aria-label="Search">
              <button >Search</button>
            </form>
          </div>
          <div >
           <a  href="/newitem" type="button">Input New Purchase</a>
          </div>
        </div>
      </div>
     <table >
       <thead>
           <tr>
            <th>Update Item</th>
           <th>Product ID</th>
           <th>Product</th>
           <th>Description</th>
           <th>Purchase Price</th>
           <th>Location</th>
           <th>List Date</th>
           <th>List Price</th>
           <th>Sold Date</th>
           <th>Sold Price</th>
           <th>Ship Date</th>
           <th>Ship Cost</th>
           </tr>
       </thead>
       {% for inventory in inventory %}
       <tr>
        <td><a class='btn btn-success btn-sm' href='/update/{{inventory.id}}'>Update</a>
        <td>{{inventory.id}}</td>
        <td>{{inventory.product}}</td>
        <td>{{inventory.description}}</td>
        <td>{{inventory.paid}}</td>
        <td>{{inventory.bin}}</td>
        <td>{{inventory.listdate}}</td>
        <td>{{inventory.listprice}}</td>
        <td>{{inventory.solddate}}</td>
        <td>{{inventory.soldprice}}</td>
        <td>{{inventory.shipdate}}</td>
        <td>{{inventory.shipcost}}</td>
       </tr>
       {% endfor %}
    </table>
{% endblock %}

search.html

{% extends 'portal/base.html' %}
{% block title %}{% endblock %}
{% block content %}
        <br>
        <div >
          <div >
           <form >
              <input  type="text" name="q" placeholder="Search" aria-label="Search">
              <button >Search</button>
            </form>
          </div>
          <div >
           <a  href="/newitem" type="button">Input New Purchase</a>
          </div>
        </div>
      </div>
     <table >
       <thead>
           <tr>
            <th>Update Item</th>
           <th>Product ID</th>
           <th>Product</th>
           <th>Description</th>
           <th>Purchase Price</th>
           <th>Location</th>
           <th>List Date</th>
           <th>List Price</th>
           <th>Sold Date</th>
           <th>Sold Price</th>
           <th>Ship Date</th>
           <th>Ship Cost</th>
           </tr>
       </thead>
       {% for inventory in inventory %}
       <tr>
        <td><a class='btn btn-success btn-sm' href='/update/{{inventory.id}}'>Update</a>
        <td>{{inventory.id}}</td>
        <td>{{inventory.product}}</td>
        <td>{{inventory.description}}</td>
        <td>{{inventory.paid}}</td>
        <td>{{inventory.bin}}</td>
        <td>{{inventory.listdate}}</td>
        <td>{{inventory.listprice}}</td>
        <td>{{inventory.solddate}}</td>
        <td>{{inventory.soldprice}}</td>
        <td>{{inventory.shipdate}}</td>
        <td>{{inventory.shipcost}}</td>
       </tr>
       {% endfor %}
    </table>
{% endblock %}

CodePudding user response:

You don't have inventory in your view's context, yet you try to use it in template:

{% for inventory in inventory %}

Change it to:

{% for inventory in searchinv %}

And it should be better.

  • Related