The bit is late, but you can cancel the form and add the display of the corresponding messages by setting form._errors
>>> f.is_valid() True >>> f._errors['my_field'] = ['This field caused a problem'] >>> f.is_valid() False >>> str(f) ... <ul class="errorlist"><li>This field caused a problem</li></ul>
I needed to do this using FormView.form_valid()
methods and models with unique fields
def form_valid(self, form): obj = User(**form.cleaned_data) try: obj.save() except IntegrityError: form._errors['username'] = ['Sorry, already taken'] return super(MyView, self).form_invalid(form) return super(MyView, self).form_valid(form)
source share