How to change UserCreationForm attributes such as error_messges, labels etc.?

Question:

I Want to change default label of django UserCreationForm which I imported from django.contrib.auth.forms

from django.contrib.auth.forms import UserCreationForm

class SignupForm(UserCreationForm):
   class Meta:
       model = models.User
       fields = ['username', 'email', 'password1', 'password2']

E.g. here, how should I change the default label or error message of username?

Asked By: sadegh

||

Answers:

UserCreationForm already has fields in it, so use labels and error_messages dictionary to override attributes inside Meta class.

According to docs:

labels is a dictionary of model field names mapped to a label.

error_messages is a dictionary of model field names mapped to a dictionary of error messages.

Try this:

class SignupForm(UserCreationForm):
   class Meta:
       model = models.User
       fields = ['username', 'email', 'password1', 'password2']
       labels={
           "username": "custom label for username"
       }
       error_messages={
           "username": {
               "required": "custom message for required"
            }
       }
Answered By: Sunderam Dubey