I have a question about django KeyError

Asked 2 years ago, Updated 2 years ago, 68 views

It implements the function of receiving file url, video fps, and video scale values from the browser using the current long Yes.

The problem occurs when you receive three values in dictionary form through axios on the front and clean_data to validate through form.

The three values are the same conditions at models.py form.py, but only url finds the key value and the rest cannot be transferred to form and becomes NoneType.

How can I work on cleaned_data without key errors for the remaining fps and scale values?

# This is the js code that passes the value in dictionary form at the front.
var sendURL = {'uploadURL': vm.inputURL,  'URL_fps_value': vm.URL_settings.URL_fpsValue, 'URL_scaleValue_select': vm.URL_settings.URL_scaleValue_select };
axios.post('/convert/urlupload/', sendURL)
...
# views.py file.
    if request.method == 'POST':
        form = UploadURLForm(json.loads(request.body))
    #json.loads(request.body) == {'key1': value1, 'key2': value2, 'key3':value3} 
    # I used request.body because I handed it over to a dictionary instead of form data at the front desk.
        if form.is_valid():
            valid_url = form.clean_uploadURL()
            url_scale = form.cleaned_data['URL_scaleValue_select']
            url_fps = form.cleaned_data['URL_fps_value']
# forms.This is a py file.
    class Meta:
        model = UploadURLmodel
        fields = ['uploadURL', 'URL_fps_value', 'URL_scaleValue_select']

    def clean_uploadURL(self):
        print("this is form")
        uploadURL = self.cleaned_data['uploadURL']
    # KeyError: 'URL_fps_value' occurs
        file_fps = self.cleaned_data['URL_fps_value']
        file_scale_value = self.cleaned_data['URL_scaleValue_select']
# models.py file.
class UploadURLmodel(models.Model):
    uploadURL = models.URLField(blank=False, null=False)
    URL_fps_value = models.IntegerField(blank=False, null=False)
    URL_scaleValue_select = models.CharField(blank=False, null=False, max_length=15)
    fileFromURL = models.FileField(blank=True, upload_to='urlmedia/%Y/%m/%d/')

++ (modified) The form is a model form, but is there anything I need to do separately when I put the parameter value in dictionary format? I definitely handed it over to the form as a dictionary, but only the url price is paid.

+++ (modified) The is_valid() function in views.py is forms.This problem occurs because the py clean_uploadURL() function progresses Same. Forms when the original is_valid() function works.Does the function in py run together?

django axios form-data

2022-09-20 22:20

1 Answers

forms.I changed the order of fields of py backwards, and it was successfully cleaned_data

The key value will be entered.

If the order of fields and the order of referring to cleaned_data are different, the cleaned_data['which key'] is

There may be an error because the value is not included when used.


2022-09-20 22:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.