0

I keep getting this error:

MultiValueDictKeyError at /search/

"Key 'name' not found in <'QueryDict: {}>"

I just started learning programming two days ago, so can someone explain in layman's terms why there's a problem and how to solve it. Thanks!

Here is the section of programming:

def NameAndOrCity(request):
    NoEntry = False
    if 'name' in request.GET and request.GET['name']:
        name = request.GET['name']
        if len(Business.objects.filter(name__icontains=name)) > 0:
            ByName = Business.objects.filter(name__icontains=name)
            q = set(ByName)
            del ByName
            ByName = q

    if 'city' in request.GET and request.GET['city']:
        city = request.GET['city']
        if len(Business.objects.filter(city__icontains=city)) > 0:
            ByCity = Business.objects.filter(city__contains=city)
            p = set(ByCity)
            del ByCity
            ByCity = p


    if len(q) > 0 and len(p) > 0:
            NameXCity = q & p
            return render_to_response('search_results.html', {'businesses':NameXCity, 'query':name})
        if len(q) > 0 and len(p) < 1:
            return render_to_response('search_results.html', {'businesses':ByName, 'query':name})
        if len(p) > 0 and len(q) < 1:
            return render_to_response('search_results.html', {'businesses':ByCity, 'query':city})
        else:
            NoResults = True
            return render_to_response('search_form.html', {'NoResults': NoResults})
    else:
        name = request.GET['name']
        city = request.GET['city']
        if len(name) < 1 and len(city) < 1:
            NoEntry = True
        return render_to_response('search_form.html', {'NoEntry': NoEntry})

EDIT

1) Business.object is my database of businesses. They are objects with attributes like name, city, etc. I'm trying to make a program that will search the businesses by their attribute(s)

2) not a duplicate post

3) how do I check to see if those keys exist before I try to use them?

3
  • 2
    I don't think that's your complete code. What's a Business.objects? Which tutorial are you using to learn Python? Commented Aug 18, 2011 at 4:07
  • possible duplicate of django MultiValueDictKeyError error, how do i deal with it Commented Aug 18, 2011 at 4:09
  • It's painful seeing len(x) < 1? len(x) == 0 is faster and more obvious, and not len(x) is faster still and in my opinion just as obvious. Similarly, len(x) > 0 is faster and better as just len(x). And with sets, lists, tuples, strs, etc. you can even drop the len() bit and just do x or not x. So things like if len(p) > 0 and len(q) < 1: could be done as if p and not q: and I at least would find it much easier to read. Commented Aug 18, 2011 at 11:07

1 Answer 1

2

It looks like the only place you could be getting this error is on this line:

name = request.GET['name']

You haven't checked if 'name' is in the request.GET dictionary before trying to access it like you did above so you will get a key error if that key doesn't exist in request.GET.

So it looks like you need to change the following section to check if the 'name' and 'city' keys exist in your request.GET dictionary before you try accessing the values:

name = request.GET['name']
city = request.GET['city']
if len(name) < 1 and len(city) < 1:
    NoEntry = True
return render_to_response('search_form.html', {'NoEntry': NoEntry})
Sign up to request clarification or add additional context in comments.

1 Comment

NoEntry = not (name or city) means NoEntry will be False if name or city is not empty, otherwise will be True. This is shorter (I am not saying that cleaner) way of doing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.