I would like to know how I can limit the file type in FileField
of Django?
I know I can add this validation to the Form. I just wish they could send videos and images in this field, nothing more.
I would like to know how I can limit the file type in FileField
of Django?
I know I can add this validation to the Form. I just wish they could send videos and images in this field, nothing more.
I found a way to identify the mime type of the file. Here is the example of the form where I limit the image and video and do not allow .bmp files:
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ("file", "name", "email", "subject", "message",)
def clean( self ):
cleaned_data = self.cleaned_data
file = cleaned_data.get( "file", None )
if file:
if file.content_type.split('/')[0] not in ['image', 'video']:
raise forms.ValidationError( 'Enviar apenas video ou imagem.' )
if file.content_type == 'image/bmp':
raise forms.ValidationError( 'Arquivos .BMP não permitidos.' )
return cleaned_data