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 try
-except
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…