I have an issue with my query string, the problem is when multiple values are selected my Django search breaks. I get this in the URL ?Query=value1&Query=Value2
. In this scenario, it’s only the last value that is searched. What I want to happen is that both values are searched (with the equivalent of an AND
operator in-between).
This is the desired result ?Query=value1+Value2
.
I’ve added my search form that uses Select2 and my Django search view below. If you need anything else let me now.
Any help would be much appreciated.
Search form on the front end
Front end code:
```
<form id="makeitso" role="search" action="" method="get">
<select class="js-example-placeholder-multiple" name="query" multiple="multiple">
<option value="Value 1">Value 1</option>
<option value="Value 2">Value 2</option>
<option value="Value 3">Value 3</option>
</select>
<script>
$(".js-example-placeholder-multiple").select2({
placeholder: "Search here",
});
</script>
<div class="form-inline justify-content-center">
<button type="submit" class="btn btn-xlarge">Search</button>
</div>
</form>
```
views.py
```
def search(request):
search_query = request.GET.get('query', None)
page = request.GET.get('page', 1)
# Search
if search_query:
search_results = TestPage.objects.live().search(search_query)
query = Query.get(search_query)
# Record hit
query.add_hit()
else:
search_results = TestPage.objects.none()
# Pagination
paginator = Paginator(search_results, 3)
try:
search_results = paginator.page(page)
except PageNotAnInteger:
search_results = paginator.page(1)
except EmptyPage:
search_results = paginator.page(paginator.num_pages)
return render(request, 'search/search.html', {
'search_query': search_query,
'search_results': search_results,
})