Last active
March 28, 2019 11:53
-
-
Save guillaumepiot/67d209710c6b226e08e3 to your computer and use it in GitHub Desktop.
DJANGO CookBook - File type validation in form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tested in Django 1.6 | |
class ImportForm(forms.Form): | |
csv_file = forms.FileField(label=_('Select CSV file')) | |
def clean_csv_file(self): | |
f = self.cleaned_data['csv_file'] | |
if not f.content_type in ['text/csv',]: | |
raise forms.ValidationError(_("The file type is not accepted.")) | |
return f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment