1

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?

3
  • How can you show the error if you never raise it in the first place? Commented Jul 3, 2017 at 9:06
  • yes, I'm asking about how to add the error to the form's errors dict?, how can I access the form? Commented Jul 3, 2017 at 13:07
  • 2
    You 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. docs.djangoproject.com/en/1.11/ref/forms/api/… Commented Jul 3, 2017 at 13:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, I'm asking about how to add the error to the form's errors dict?, how can I access the form?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.