I'm trying to add validation to a filefiled, I used this snippet and it works, but the problem is that it raises an exception when the file extension does not match, but I just want it to show form validation errors, not raising errors.
How can I do that?
Asked
Active
Viewed 524 times
1

Shai
- 111,146
- 38
- 238
- 371

M. Dhaouadi
- 617
- 10
- 27
-
How can you show the error if you never raise it in the first place? – Håken Lid Jul 03 '17 at 09:06
-
yes, I'm asking about how to add the error to the form's errors dict?, how can I access the form? – M. Dhaouadi Jul 03 '17 at 13:07
-
2You don't need to do that yourself. Any `ValidationError` raised when you call `form.is_valid()` will be caught and added to the error dict. That's how it's supposed to work. https://docs.djangoproject.com/en/1.11/ref/forms/api/#django.forms.Form.is_valid – Håken Lid Jul 03 '17 at 13:17
1 Answers
0
Simply replace the raise forms.ValidationError(_('Filetype not supported.'))
in line 38 to the action you want to occur when the Filetype
is not supported.
Python raise
raises an exception, replacing it with a different command will stop python from "raising" that specific exception.

Shai
- 111,146
- 38
- 238
- 371
-
yes, I'm asking about how to add the error to the form's errors dict?, how can I access the form? – M. Dhaouadi Jul 03 '17 at 13:03