django-formset icon indicating copy to clipboard operation
django-formset copied to clipboard

UploadedFileInput Error when modifying or deleting model

Open jamiecash opened this issue 1 year ago • 5 comments

When using the UploadedFileInput.On creation, uploaded image is stored successfully, however when modifying the model but not changing the image, or when deleting the model, the following validation error is shown: No file was submitted. Check the encoding type on the form.

The form

class SubjectForm(forms.ModelForm):
    """
    Form to create and edit lesson subjects.
    """

    name = fields.CharField(max_length=100, required=True)
    image = fields.ImageField(
        label="Image",
        widget=UploadedFileInput(attrs={
            'max-size': 1024 * 1024,
        }),
        help_text="Files larger than 1MB are not supported.",
        required=False,
    )

    class Meta:
        model = models.Subject
        fields = ['name', 'image']

The model

class Subject(models.Model):
    """
    A subject being taught.
    """
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='subjects/images/', blank=True, null=True)

    def __str__(self):
      return self.name

    class Meta:
      db_table = 'lessons_subject'
      ordering = ('name',)

When debugging, the FormViews form_valid method is not reached.

jamiecash avatar Jul 14 '24 01:07 jamiecash