[Answered ]-Exceptions in django not showing

1👍

You reraise the erro before you add the message, so messages.error is "dead code". You thus should first add the message, and then raise the error, so:

try:
    user = User.objects.create_user(username = username, password = password)
    messages.success(request, 'successfully registered')
except Error as e:
    messages.error(request, str(e))
    raise ValidationError(e)

That being said, create_user will save the user, so that means that you need to wrap the .create_user call in a tryexcept statement. Furthermore it is not clear to me what you want to happen by raising a ValidationError. A view is not supposed to raise a ValidationError, in that case you normally rerender the page with a form that shows the errors.

Leave a comment