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?

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Kbob
  • 177
  • 1
  • 3
  • 2
    I don't think that's your complete code. What's a `Business.objects`? Which tutorial are you using to learn Python? – Greg Hewgill Aug 18 '11 at 04:07
  • possible duplicate of [django MultiValueDictKeyError error, how do i deal with it](http://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – johnsyweb Aug 18 '11 at 04: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 `set`s, `list`s, `tuple`s, `str`s, 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. – Chris Morgan Aug 18 '11 at 11:07

1 Answers1

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})
randlet
  • 3,628
  • 1
  • 17
  • 21
  • `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. – Tadeck Mar 21 '12 at 15:17