[Django]-Django: request.GET and KeyError

60👍

Your server should never produce a 500 error page.

You can avoid the error by using:

my_param = request.GET.get('param', default_value)

or:

my_param = request.GET.get('param')
if my_param is None:
    return HttpResponseBadRequest()

12👍

Yes, you should check for KeyError in that case. Or you could do this:

if 'param' in request.GET:
    my_param = request.GET['param']
else:
    my_param = default_value

1👍

How about passing default value if param doesn’t exist ?

my_param = request.GET.get('param', 'defaultvalue')

Leave a comment