Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"
            }
       }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading