我正在创建搜索功能。用户将在其中输入名称和城市。结果应显示为:应显示该城市可用的名称。 view.py
def search(request):
if request.method == 'GET':
srch = request.GET['srh']
srch1 = request.GET['srh1']
if srch:
match = demo.objects.filter(Q(name__icontains=srch))
if srch1 in match:
match2 = demo.objects.filter(Q(city__icontains=srch1))
if match2:
return render(request, 'listing-search.html', {'sr':match2})
else:
print('no result found')
else:
return HttpResponseRedirect('')
return render(request, 'listing-search.html')
模板
<form action="search/" method="get">
<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">
</div>
<div class="input-group input-group-2">
<span class="input-group-addon" id="basic-addon2">Location</span>
<input type="text" name="srh1" class="form-control" placeholder="Ex: Shahabad" aria-describedby="basic-addon2" list="suggest-location">
</div>
<button class="btn btn-default" type="submit"><i class="fa fa-search" style="font-size: 25px"></i></button>
<div class="fix"></div>
</form>
Models.py
class demo(models.Model):
name = models.CharField(max_length=100)
city = models.CharField(max_length=100, null=True,)
def __str__(self):
return self.name
答案 0 :(得分:0)
代替此:
srch = request.GET['srh']
srch1 = request.GET['srh1']
使用此:
srch = request.GET.get('srh')
srch1 = request.GET.get('srh1')