Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Django throw a KeyError on this form validation?
    primarykey
    data
    text
    <p>Here's the code: </p> <pre><code>... class Meta: model = Card def clean_video_url(self): video_url = self.cleaned_data['video_url'] if video_url != '' and len(video_url) != YOUTUBE_VIDEO_URL_LENGTH: pos = string.find(video_url, YOUTUBE_VIDEO_URL_IDENTIFIER) identifier_length = len(YOUTUBE_VIDEO_URL_IDENTIFIER) if pos == -1: raise forms.ValidationError(_('youtube-url-not-valid')) video_url = video_url[pos+identifier_length:pos+identifier_length+YOUTUBE_VIDEO_URL_LENGTH] return video_url ... def clean(self): video_url = self.cleaned_data['video_url'] field1 = self.cleaned_data['field1'] if video_url == '' and field1 == '': raise forms.ValidationError(_('must-fill-video-url-or-front')) return self.cleaned_data </code></pre> <p>The most disturbing thing is that it works (submits and persists in the database) in almost all situations. It doesn't work when I write dummy text like 'aeuchah' in the <strong>video_url field</strong>, but instead it throws:</p> <pre><code>Exception Type: KeyError Exception Value: 'video_url' </code></pre> <p>I re-read my clean_video_url method and went to see what the variables were with a debug tool like pdb.set_trace, but I can't find the problem. </p> <p><strong>UPDATE</strong>: <strong>As Marius Grigaitis and Davide R. said, the clean method is called after all the individual field methods are done. clean_video_url raised a ValidationError and returned nothing, so the clean method found nothing to work with and raised a KeyError.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload