Django/PostgreSQL не отображает результат поиска
Я не могу получить никаких результатов поиска при поиске элементов. Я использую Postgresql и Django. Ниже приведен мой код. Я не уверен, правильно ли я делаю поисковый запрос или просто не могу отобразить результаты.
У меня есть строка поиска в файле "inventory_management.html". Я пытаюсь сделать так, чтобы пользователь искал предмет, а затем выводился список отображаемых предметов.
Любая помощь будет очень признательна!
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 (где находится строка поиска)
{% extends 'portal/base.html' %}
{% block title %}{% endblock %}
{% block content %}
<br>
<div class="row">
<div class="col">
<form class="d-flex" role="search" action="/search" method="get">
<input class="form-control me-2" type="text" name="q" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success">Search</button>
</form>
</div>
<div class="col">
<a class="btn btn-primary me-md-2" href="/newitem" type="button">Input New Purchase</a>
</div>
</div>
</div>
<table class="table table-striped">
<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 class="row">
<div class="col">
<form class="d-flex">
<input class="form-control me-2" type="text" name="q" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success">Search</button>
</form>
</div>
<div class="col">
<a class="btn btn-primary me-md-2" href="/newitem" type="button">Input New Purchase</a>
</div>
</div>
</div>
<table class="table table-striped">
<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 %}
У вас нет inventory
в контексте представления, но вы пытаетесь использовать его в шаблоне:
{% for inventory in inventory %}
Измените его на:
{% for inventory in searchinv %}
И это должно быть лучше.