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 %}

它应该更好。

最新更新