Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot use None as a query value error when i try to view page without search query

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})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading