2👍
✅
You should return form_invalid
in the except clause rather than using render_to_response
def form_valid(self, form):
form.instance.user = self.request.user
try:
return super(DataCreate, self).form_valid(form)
except IntegrityError:
form.add_error('unique_name','You already have a user by that name')
return self.form_invalid(form)
Source:stackexchange.com