在 django 搜索中没有得到结果,但个人搜索工作正常
Posted
技术标签:
【中文标题】在 django 搜索中没有得到结果,但个人搜索工作正常【英文标题】:not getting results in django search but individual searching is working fine 【发布时间】:2020-07-07 16:08:02 【问题描述】:我的问题是,当我向单个搜索字段添加简单功能时,它对于单个搜索字段运行良好,例如在为位置字段添加功能后,它可以按我的意愿工作,但是在为另一个字段添加另一个功能后,它没有显示任何内容即使第一个字段也没有显示结果,我认为这是一个非常愚蠢的问题,但我无法弄清楚,请帮我编码! (我添加了 html 和 views.py 代码,如果您需要其他任何东西,请告诉我 Guyz)
html代码
<form action="% url 'search-page' %" method="">
<div class="row">
<div class="col-12 col-lg-10">
<div class="row">
<div class="col-12 col-md-6 col-lg-3">
<select name="location" id="location" class="form-control">
<option value="">Location</option>
% for key,value in location_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="types" id="types" class="form-control">
<option value="all-types">All Types</option>
% for key,value in property_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="city" id="city" class="form-control">
<option value="01">All City</option>
% for key,value in city_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="status" id="all" class="form-control">
<option value="01">Status</option>
% for key,value in status_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="bedroom" id="bedrooms" class="form-control">
<option value="">Bedrooms</option>
% for key,value in bedroom_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="bathroom" id="bathroom" class="form-control">
<option value="Bathroom">Bathroom</option>
% for key,value in bathroom_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="price" id="bathroom" class="form-control">
<option value="Bathroom">Price</option>
% for key,value in price_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<div class="col-12 col-md-6 col-lg-3">
<select name="size" id="bathroom" class="form-control">
<option value="Bathroom">Lot Size</option>
% for key,value in lot_area_choices.items %
<option value=" key "> value </option>
% endfor %
</select>
</div>
<!-- <div class="col-12 col-md-6 col-lg-3">
<div class="slider-range mb-15">
<div class="range-price text-white">Price [30000 - 150000]$</div>
<div class="slider-range-price ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"
data-min="0" data-max="200000" data-unit="$" data-value-min="30000"
data-value-max="150000" data-label-result="Price">
<div class="ui-slider-range ui-widget-header ui-corner-all"></div>
<span class="ui-slider-handle ui-state-default ui-corner-all"
tabindex="0"></span>
<span class="ui-slider-handle ui-state-default ui-corner-all"
tabindex="0"></span>
</div>
</div>
</div> -->
<!-- <div class="col-12 col-md-6 col-lg-3">
<div class="slider-range mb-15">
<div class="range-size text-white">Size [9762 - 72063]sqFt</div>
<div data-min="0" data-max="98623" data-unit="sqFt"
class="slider-range-size ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"
data-value-min="9762" data-value-max="72063" data-label-result="Size">
<div class="ui-slider-range ui-widget-header ui-corner-all"></div>
<span class="ui-slider-handle ui-state-default ui-corner-all"
tabindex="0"></span>
<span class="ui-slider-handle ui-state-default ui-corner-all"
tabindex="0"></span>
</div>
</div>
</div> -->
</div>
</div>
<div class="col-12 col-lg-2">
<button type="submit" class="btn rehomes-search-btn">Search</button>
</div>
</div>
</form>
views.py
def search(request):
property_list = Property.objects.order_by('-date_added')
# Search_location
if 'location' in request.GET:
location = request.GET['location']
if location:
property_list = property_list.filter(state__iexact=location)
# Search_types
if 'types' in request.GET:
types = request.GET['types']
if types:
property_list = property_list.filter(property_types__iexact=types)
# Search_status
if 'bedroom' in request.GET:
bedroom = request.GET['bedroom']
if bedroom:
property_list = property_list.filter(bedroom__lte=bedroom)
context =
'property_data': property_list,
'location_choices': choices.location_choices,
'property_choices': choices.property_choices,
'city_choices': choices.city_choices,
'status_choices': choices.status_choices,
'bedroom_choices': choices.bedroom_choices,
'bathroom_choices': choices.bathroom_choices,
'price_choices': choices.price_choices,
'lot_area_choices': choices.lot_area_choices
return render(request, 'property/search.html', context)
【问题讨论】:
如果没有显示字段或结果,它会显示什么?你能确定它在模板中的哪个点失败了吗?您还可以安装 django 调试工具栏并查看模板在渲染时的上下文。 实际上它没有显示结果,因为您可以看到有 3 个不同的搜索字段的功能,如果我注释掉任何 2 个字段的功能,那么它可以正常工作, 主要是如果我只为单个字段添加搜索功能,它正在工作,但是当我为另一个字段添加相同的功能时,它不显示结果,即使第一个字段显示结果但之后添加另一个 1 也不会显示结果,但所有字段功能都相同,您怎么看? 我会安装 Django 调试工具栏并检查发送到模板的上下文。 【参考方案1】:if 'location' in request.GET:
location = request.GET['location']
if location:
property_list = property_list.filter(state__iexact=location)
# Search_types
if 'types' in request.GET:
types = request.GET['types']
if types:
property_list = property_list.filter(property_types__iexact=types)
# Search_status
if 'bedroom' in request.GET:
bedroom = request.GET['bedroom']
if bedroom:
property_list = property_list.filter(bedroom__lte=bedroom)
【讨论】:
以上是关于在 django 搜索中没有得到结果,但个人搜索工作正常的主要内容,如果未能解决你的问题,请参考以下文章
如何在 django 中显示来自 ajax 搜索结果的产品?
Django 中的两个与搜索相关的错误:当没有搜索项时:“reduce() of empty sequence with no initial value”。在任何搜索词上:没有结果
Django trigram_similar搜索没有返回结果(带有Postgresql 10.5后端的Django 2.1)