Django: request.GET and KeyError Django: request.GET and KeyError python python

Django: request.GET and KeyError


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


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


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

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