Python 仅输入一个搜索参数时,Django for Search中的request.GET不起作用
这是我的搜索功能: 当我使用它时,我无法通过仅放置关键字或城市或我在这里使用的任何其他参数来搜索 它没有可用的列表。 但如果我删除任何搜索参数,则任何搜索参数都只能使用一个参数,如: 如果我删除了除#关键字以外的所有其他关键字,我的搜索就会成功Python 仅输入一个搜索参数时,Django for Search中的request.GET不起作用,python,django,Python,Django,这是我的搜索功能: 当我使用它时,我无法通过仅放置关键字或城市或我在这里使用的任何其他参数来搜索 它没有可用的列表。 但如果我删除任何搜索参数,则任何搜索参数都只能使用一个参数,如: 如果我删除了除#关键字以外的所有其他关键字,我的搜索就会成功 # search function using request.GET def search(request): # for making search option dynamic as per search of user queryset
# search function using request.GET
def search(request):
# for making search option dynamic as per search of user
queryset_list = Listing.objects.order_by('-list_date')
#Keywords
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if 'keywords':
queryset_list = queryset_list.filter(description__icontains=keywords)
#City
if 'city' in request.GET:
city = request.GET['city']
if 'city':
queryset_list = queryset_list.filter(city__iexact=city)
# Bedrooms
if 'bedrooms' in request.GET:
bedrooms = request.GET['bedrooms']
if 'bedrooms':
queryset_list = queryset_list.filter(bedrooms__lte=bedrooms)
# Price
if 'price' in request.GET:
price = request.GET['price']
if 'price':
queryset_list = queryset_list.filter(price__lte=price)
context = {
'state_choices': state_choices,
'bedroom_choices': bedroom_choices,
'price_choices': price_choices,
'listings': queryset_list,
'values': request.GET
}
return render(request, 'listings/search.html', context)
这是我的模板文件。。。。。用于搜索
{%extends 'base.html'%}
<!---Import Humanaize---->
{%load humanize %} {% block title %} | Search Results {%endblock%}
<!---- Start Block Content-->
{%block content%}
<section id="showcase-inner" class="showcase-search text-white py-5">
<div class="container">
<div class="row text-center">
<div class="col-md-12">
<form action="{%url 'search' %}">
<!-- Form Row 1 -->
<div class="form-row">
<div class="col-md-4 mb-3">
<label class="sr-only">Keywords</label>
<input type="text" name="keywords" class="form-control" placeholder="Keyword (Pool, Garage, etc)" value="{{values.keywords}}">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">City</label>
<input type="text" name="city" class="form-control" placeholder="City" value="{{values.city}}">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">State</label>
<select name="state" class="form-control">
<option selected="true" disabled="disabled">State (All)</option>
<!--loop through the key and value pairs-->
{%for key, value in state_choices.items %}
<option value="{{ key }}"
{% if key == values.state %}
selected
{%endif%}
>
{{ value }}</option>
{%endfor%}
</select>
</div>
</div>
<!-- Form Row 2 -->
<div class="form-row">
<div class="col-md-6 mb-3">
<label class="sr-only">Bedrooms</label>
<select name="bedrooms" class="form-control">
<option selected="true" disabled="disabled">Bedrooms (Any)</option>
<!--loop through the key and value pairs-->
{%for key, value in bedroom_choices.items %}
<option value="{{ key }}"
{% if key == values.bedrooms %}
selected
{%endif%}
>{{ value }}</option>
{%endfor%}
</select>
</div>
<div class="col-md-6 mb-3">
<select name="price" class="form-control">
<option selected="true" disabled="disabled">Max Price (All)</option>
<!--loop through the key and value pairs-->
{%for key, value in price_choices.items %}
<option value="{{ key }}"
{% if key == values.price %}
selected
{%endif%}
>
{{ value }}</option>
{%endfor%}
</select>
</div>
</div>
<button class="btn btn-secondary btn-block mt-4" type="submit">Submit form</button>
</form>
</div>
</div>
</div>
</section>
<!-- Breadcrumb -->
<section id="bc" class="mt-3">
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="{%url 'index'%}">
<i class="fas fa-home"></i> Home</a>
</li>
<li class="breadcrumb-item">
<a href="{%url 'listings' %}">Browse Listings</a>
</li>
<li class="breadcrumb-item active"> Search Results</li>
</ol>
</nav>
</div>
</section>
<!-- Listings -->
<section id="listings" class="py-4">
<div class="container">
<div class="row">
{%if listings %} {% for listing in listings %}
<!-- Listing 1 -->
<div class="col-md-6 col-lg-4 mb-4">
<div class="card listing-preview">
<img class="card-img-top" src="{{ listing.photo_main.url }}" alt="">
<div class="card-img-overlay">
<h2>
<span class="badge badge-secondary text-white">${{listing.price | intcomma}}</span>
</h2>
</div>
<div class="card-body">
<div class="listing-heading text-center">
<h4 class="text-primary">{{listing.title}}</h4>
<p>
<i class="fas fa-map-marker text-secondary"></i> {{listing.city}} {{listing.state}}, {{listing.zipcode}}</p>
</div>
<hr>
<div class="row py-2 text-secondary">
<div class="col-6">
<i class="fas fa-th-large"></i> Sqft: {{listing.sqft}}</div>
<div class="col-6">
<i class="fas fa-car"></i> Garage: {{listing.garage}}</div>
</div>
<div class="row py-2 text-secondary">
<div class="col-6">
<i class="fas fa-bed"></i> Bedrooms: {{listing.bedrooms}}</div>
<div class="col-6">
<i class="fas fa-bath"></i> Bathrooms: {{listing.bathrooms}}</div>
</div>
<hr>
<div class="row py-2 text-secondary">
<div class="col-12">
<i class="fas fa-user"></i> {{listing.realtor}}</div>
</div>
<div class="row text-secondary pb-2">
<div class="col-6">
<i class="fas fa-clock"></i> {{listing.list_date | timesince}}</div>
</div>
<hr>
<a href="{% url 'listing' listing.id %}" class="btn btn-primary btn-block">More Info</a>
</div>
</div>
</div>
{%endfor%}
<!----if above is not true------>
{%else%}
<div class="col-md-12">
<p>
No Listings Available
</p>
</div>
<!------end of the if statement ------>
{%endif%}
</div>
</div>
</section>
{%endblock%}
{%extends'base.html%}
{%load humanize%}{%block title%}|搜索结果{%endblock%}
{%block content%}
关键词
城市
陈述
国家(全部)
{%表示键,值位于状态_choices.items%}
{{value}}
{%endfor%}
卧室
卧室(任何)
{%为键,值在0.items%}
{{value}}
{%endfor%}
最高价格(全部)
{键的百分比,价格中的值_choices.items%}
{{value}}
{%endfor%}
提交表格
搜索结果
{%if listings%}{%if listings%}
${{listing.price | intcomma}}
{{listing.title}
{{listing.city}{{listing.state},{{{listing.zipcode}
Sqft:{{listing.Sqft}}
车库:{{listing.Garage}
卧室:{{listing.beddooms}
浴室:{{listing.Bathrooms}
{{listing.realtor}
{{listing.list_date|timesince}
{%endfor%}
{%else%}
没有可用的列表
{%endif%}
{%endblock%}
据我所见,您的视图中有一个小错误。像这样的陈述
if 'keywords' in request.GET:
不会检查“关键字”是否为空。只要表单中有一个名为“关键字”的字段,我将始终返回True
那么你的支票
if 'keywords':
将始终返回True
,因为您没有检查变量,而是检查字符串。你的意思可能是
if keywords:
检查关键字是否包含某些内容
因此,当前发生的情况是,由于所有检查都返回True
,您正在使用大量空字符串筛选查询集,这将返回一个空查询集
因此,从检查中删除'
,如if'keywords':
和其他类似的if
语句
如果需要,您还可以删除请求中的
If'keywords'。GET:
和类似的内容,因为它们总是返回True
您可以更改请求吗?在请求中获取['keyword']部分。GET.GET('keyword'),然后重试?出于好奇,这是否不会获取整个表的行值,然后过滤RAM中的结果集?至少在小桌子外面是这样吗?除了通过管理员,我很少使用ORM,所以可能是错误的。