I have 2 HTML submission fields under one submit button, one of which is optional. I’m using the ‘get’ method to retrieve submitted data, but I want the optional field to have a default value just in case the user submits an empty form. If I don’t do this, I get a MultiValueDictKeyError.
if request.method == 'GET':
# required
name = request.GET['name']
# optional
color = request.GET['color']
I think a possible solution is try / except for every field, but is there a more elegant method? I know for get.post() you can do something like
color = request.POST.get('color', False)
But this doesn’t seem to work for just request.get()
Any ideas?
Thanks,
fertileneutrino
Confused here…
request.get()won’t work, butrequest.GET.get()should. Did you just mistype or were you actually usingrequest.get()?