I have a working search form in my base.html but when itry to access the listing page without passing a search query i get a "Cannot use None as a query value" error. views.py
def listing(request):
if request.method == 'GET':
search = request.GET.get('search')
propertys = Paginator(Property.objects.filter(property_name__icontains =search).order_by('-listed_on'), 2)
page = request.GET.get('page')
propertys = propertys.get_page(page)
nums = "p" * propertys.paginator.num_pages
return render(request,"listing.html",{"propertys":propertys, "nums": nums, 'search':search})
else:
p = Paginator(Property.objects.order_by('-listed_on'), 2)
page = request.GET.get('page')
propertys = p.get_page(page)
nums = "p" * propertys.paginator.num_pages
return render(request,"listing.html", {"propertys":propertys, "nums": nums})
CodePudding user response:
You can't search by None
in that way, but you can simply check first if search
has a value other than None
before you actually try it:
Replace
propertys = Paginator(Property.objects.filter(property_name__icontains =search).order_by('-listed_on'), 2)
with
if search:
propertys = Paginator(Property.objects.filter(property_name__icontains =search).order_by('-listed_on'), 2)