0

I have the following form in my forms.py. For some reason the override of label and help_text for username field WORKS and (both) the password fields doesn't.

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'password1', 'password2']
        labels = {
            'username': 'My Username Label',
            'password1': 'My Password1 Label',
            'password2': 'My Password2 Label',
        }
        help_texts = {
            'username': 'My username help_text',
            'password1': 'My password1 help_text',
        }

When rendered the default django label/help_texts are showed for password fields:

Your password can’t be too similar to your other personal information.
Your password must contain at least 8 characters.
...

I've already read those answers, but they weren't helpful: Q1 Q2 Q3

I've also read the docs, and there is a note here (green note lowering the page) that seems to be related, but I don't really understand it. It mentions Fields defined declaratively, which I'm not doing. Also wouldn't explain why it works for username and not for password1.

Justcurious
  • 1,952
  • 4
  • 11
  • 17
  • Can you share your template and view code – Victor Apr 19 '20 at 14:32
  • 1
    `username` is an actual field on the model, hence the override in Meta works. `password1` is a field defined only on the form and the help text is genrated from the validators set in the password validation settings. What's wrong with the original help_text? – user2390182 Apr 19 '20 at 14:40

2 Answers2

4

Override the __init__(...) method of the form class,

class CreateUserForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password1'].label = 'password1 label'
        self.fields['password2'].label = 'password2 label'

        self.fields['password1'].help_text = 'password1 help_text'
        self.fields['password2'].help_text = 'password2 help_text'
JPG
  • 82,442
  • 19
  • 127
  • 206
  • That worked great, thank you. Do you mind giving a brief explanation on to why? – Justcurious Apr 19 '20 at 14:43
  • checkout @schwobaseggl 's [comment](https://stackoverflow.com/questions/61306006/how-to-override-help-text-and-label-for-password-form-in-django/61306119?noredirect=1#comment108454024_61306006) – JPG Apr 19 '20 at 14:55
0

I had same concern as well. I tried many ways, but the best one is to write your own validator. Let's say we wanted to change text of error "This password is entirely numeric." to something else line "پسورد شما کاملا عدد است. لطفا آن را تغییر دهید.".

suppose my app is auth_app in my project.

create a validator.py in your app:

import re

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _


class CustomNumericPasswordValidator:
    """
    Validate whether the password is alphanumeric.
    """
    def validate(self, password, user=None):
        if password.isdigit():
            raise ValidationError(
                _("پسورد شما کاملا عدد است. لطفا آن را تغییر دهید."),
                code='password_entirely_numeric',
            )

    def get_help_text(self):
        return _('Your password can’t be entirely numeric.')

then go to settings.py and replace your validator with default

{
    'NAME': 'auth_app.validators.CustomNumericPasswordValidator',
},

save and enjoy the change.

Here is a test to what we defined as new error message

Sadegh Karimi
  • 21
  • 1
  • 6