我正在学习Django的搜索功能。我想在单独的页面上显示我的搜索结果。例如。 listingsearch
home-v3.html页面
<form method="POST" action="/listingsearch/">
{% csrf_token %}
<div class="input-group input-group-1">
<span class="input-group-addon" id="basic-addon1">Find</span>
<input type="text" name= "srh" class="form-control" placeholder="Business Name Type Karo" aria-describedby="basic-addon1" list="find">
<datalist id="find">
<option value="Food">
<option value="Service">
<option value="Hotel">
</datalist>
</div>
<button class="btn btn-default" type="submit"><i class="fa fa-search" style="font-size: 25px"></i></button>
<div class="fix"></div>
</form>
listing-search.html
<div>
{% if sr %}
{% for k in sr %}
<table border="0px" width="300px"
<tr>
<td>
{{ k.business_name }}
</td>
</tr>
{% endfor %}
{% endif %}
</div>
查看页面
def homesearch(request):
if request.method == 'POST':
srch = request.POST['srh']
if srch:
match = business_info.objects.filter(Q(business_name))
if match:
return render(request, 'listing-search.html', {'sr':match})
else:
messages.error(request,'no result found')
else:
return HttpResponseRedirect('/homesearch/')
return render(request, 'home-v3.html')
URL页面
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', business_info_form),
path('signup/', signup),
path('homesearch/', homesearch),
path('listingsearch/', listingsearch)
]
Models.py页面
class business_info(models.Model):
business_name = models.CharField(max_length=100, null=True)
email_id = models.EmailField(max_length=60, null=True)
mobile = models.CharField(max_length=10, null=True)
def __str__(self):
return self.business_name
我想知道如何使我的搜索更好。请帮忙。