Zarrie
Zarrie

Reputation: 333

Deal with django ImageField current value displayed in forms

I'm trying to remove the clear checkbox in Django's ImageField and remove the displayed current value of the file. Tha approach I tried is to replace the widget as proposed here How to not render django image field currently and clear stuff? but the result is that I get ValidationError : "Upload a valid image. The file you uploaded was either not an image or a corrupted image.". Though the validation error the image get's uploaded, but I do not get redirected to the success page and an error is being rendered in the form. What's the recommended way to remove the current value in the ImageField?

Upvotes: 0

Views: 300

Answers (1)

Zarrie
Zarrie

Reputation: 333

A solution that works for me, though I'm not sure how good is it.

profile_picture = forms.FileField(label='Upload profile picture', widget=forms.FileInput,validators=[validators.validate_image_file_extension], required=False)

I replaced ImageField with FileField and then added the ImageField default validator with FileInput widget and then added 'accept': 'image/*' to that widget, so the select shows only images. In this way I mimic the ImageField, but no error occurs.

Upvotes: 1

Related Questions