When writing a user registration form in Django, you are likely to encounter this error message:
A user with that Username already exists.
This happens when a new user wants to register with a name that is already stored in the database. The message itself is self explainatory but what I need is to display this message in Chinese. According to Django's documentation, I should be able to do this:
But this didn't work. It turns out that Django's CharField only accepts the following error message keys:
Error message keys: required, max_length, min_length
Thanks to this StackOverflow post, here is how Django developers solved this problem in the UserCreationForm, we can adopt their solution to this situation:
Now when you try to enter a duplicate username, you will see the custom error message being shown instead of the default one :)