Home > Back-end >  How to change UserCreationForm attributes such as error_messges, labels etc.?
How to change UserCreationForm attributes such as error_messges, labels etc.?

Time:10-09

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?

CodePudding user response:

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"
            }
       }
  • Related