0

This error that I am getting is very weird. I have a form:

class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = ['first_name', 'last_name', 'description', 'gender', 'language', 
    ]
    widgets = {
        'language': forms.RadioSelect,
    }

So as you can see, the language is using the Radio button method. Then in my models.py, I specify the field for the languages like so:

LANGUAGE = (('AR', 'Arabic'), ('FR', 'French'), ('ES', 'Spanish'))
language = models.CharField(max_length=20, choices=LANGUAGE, blank=False, default=None)

Then in my views.py, I have something like this:

    if request.method == "POST":
        # Model
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        gender = request.POST['gender']
        language = request.POST['language']

        Profile.objects.create(
                    first_name=first_name,
                    last_name=last_name,
                    description=description,
                    gender=gender,
                    language=language,
        )

So for the form, if I select an entry for language, then it all works fine and dandy. However, if I leave the language field empty (I don't select anything), it gives me MultiValueDictKeyError.

Here is the traceback:

Traceback:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/andyxu/Documents/ece496-web/matchalgorithm/views.py" in forms
  31.         language = request.POST['language']

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__
  85.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /matchalgorithm/forms/
Exception Value: "'language'"
anderish
  • 1,709
  • 6
  • 25
  • 58
  • 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) – Brian H. Jan 24 '17 at 15:45

1 Answers1

3

You should not be accessing request.POST directly. The whole point of a form is that it deals with this for you, so that you can check form.is_valid() and then access form.cleaned_data['whatever'].

Indeed, in your case you shouldn't even need to do that, because you can just call the form save method.

if request.method == "POST":
    form = ProfileForm(request.POST)
    if form.is_valid():
        profile = form.save(commit=False)
        profile.user = request.user
        profile.save()
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895