2đź‘Ť
business_list
is a function that takes a request and a parameter called “param” (should probably give it a better name as to make it a little less generic). In Django, the URL routes can define what values get passed to the parameters of the view functions.
In the situation above, when a user goes to /Miami
, Django will try to match the string "Miami"
with any of the regular expressions defined in the URL routes. In this case, the matching expression is ^(?P<param>\w+)$
. When this match is made, the string Miami
is captured into param
.
Django will then call business_list(request, param="Miami")
. Note that there were no query parameters passed in the URL (e.g., /Miami?color=blue
).
The issue in the code you’ve written above is that you’re checking to see not if param
exists, but rather that param
was passed in the query parameters. Try going to the URL /Miami?param=test
and it’ll probably work the way that you expected.
The real fix here is to not reference request.GET
, because using GET parameters is exactly what you’re trying to avoid. So, instead of
if param in request.GET:
param = request.GET.get('param')
if queryset.filter(city__iexact=param).exists():
queryset = queryset.filter(city__iexact=param)
elif queryset.filter(category__iexact=param).exists():
queryset = queryset.filter(category__iexact=param)
print queryset
Just do:
if param:
if queryset.filter(city__iexact=param).exists():
queryset = queryset.filter(city__iexact=param)
elif queryset.filter(category__iexact=param).exists():
queryset = queryset.filter(category__iexact=param)