Answer the question
In order to leave comments, you need to log in
How to make a request to the database depending on the GET parameters?
Good evening!
There is a function in views.py:
def shows(request):
form = SelectParams(request.GET)
if not form.is_valid():
return HttpResponseBadRequest('You have entered the wrong data.')
country = form.cleaned_data['country']
state = form.cleaned_data['state']
city = form.cleaned_data['city']
data = Model.objects.filter(country=country, city=city, state=state) # TODO
return render(request, 'template/index.html', {
'data' : data,
'form' : form,
})
Answer the question
In order to leave comments, you need to log in
With an explicit indication of the city, it makes no sense to filter by country and state; with an explicit indication of the state, there is no point in filtering by country. The code may look something like this (I, of course, have no way to check):
location = form.cleaned_data
city, state, country = location['city'], location['state'], location['country']
kwargs = ({'city': city} if city
else ({'state': state} if state
else {'country': country} if country))
data = Model.objects.filter(**kwargs)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question