I have come across a problem when I submit an empty radio input. If I select a choice, the form functions fine; however, if I leave it blank, I get the following error --
MultiValueDictKeyError at /
Key 'like' not found in <QueryDict:...
I've tried several solutions, including using a dict.get on the mentioned field 'like' and deleting the column in the database -- it seems to be a problem in the forms module.
Here is my code:
In forms.py --
from django import forms
class PictureForm(forms.Form):
like = forms.ChoiceField(widget=forms.RadioSelect(), choices=(
[('yes','yes'), ('no','no'),]),)
name = forms.CharField()
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
And in views.py
def index2(request):
if request.method == 'POST':
form = PictureForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
Picture.objects.create(like=cd['like'], name=cd['name'], email=cd['email'], message=cd['message'])
return HttpResponseRedirect ('/thanks/')
else:
form = PictureForm()
return render_to_response('index2.html', {'form':form}, context_instance=RequestContext(request))
I would like to have it so there is obviously no error when the radio submitted blank -- and
1) how to allow a blank submission, and
2) how to prompt an error message.
In addition, the validations are not working on this form (I've done previous tutorials, and this is the first time the validations aren't automatically working).
Thank you.