[Fixed]-How to raise a error inside form_valid method of a CreateView

46👍

As a general rule, model validation should go into model fields validators or model’s clean method. Form validation should go into form’s clean or clean_<field> methods.

If you need to validate things in your view’s form_valid, you can use form.add_error and then, instead of redirecting (or returning super(cloud, self).form_valid(form), which redirects anyways), you could return super(cloud, self).form_invalid(form).

Check:
https://docs.djangoproject.com/en/3.1/ref/forms/api/#django.forms.Form.add_error

Leave a comment