How to raise a error inside form_valid method of a CreateView

我与影子孤独终老i 提交于 2019-12-04 03:31:20

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/1.9/ref/forms/api/#django.forms.Form.add_error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!