Added more lines to existing password help_text

How do I add additional lines to existing help_text ?
self.fields['password1'].help_text is as follows :

<ul>
<li>Your password can’t be too similar to your other personal information.</li>
<li>Your password must contain at least 8 characters.</li>
<li>Your password can’t be a commonly used password.</li>
<li>Your password can’t be entirely numeric.</li>
</ul>

self.fields['password1'].help_text += 'password1 more criteria' is adding text after the closing <ul> - is there some way to just add to the array as <li> instead of self.fields['password1'].help_text = self.fields['password1'].help_text[0:-5] + '<li>More text</li>' + '</ul>' ?

When and where are you looking to extend this list?

This list is built dynamically by the password validators when a password is submitted. See django.contrib.auth.password_validation. Each validator is run, and if the password fails that validator, the message is added to the help_texts list. If you add new validators, you have the opportunity at that point to add elements to that list.

Hello
I have a topic before this question
can you help me?

Please to not spam other topics. If someone thinks they are able to answer your question, they will do so.

forms.py :

class UserCreationForm(UserCreationForm):
    
    first_name = forms.CharField(max_length=50)
    last_name = forms.CharField(max_length=50)
    email = forms.EmailField(max_length=255)

    def __init__(self, *args, **kwargs):
        super(UserCreationForm, self).__init__(*args, **kwargs)        
        
        self.fields['email'].help_text = 'Your email address will not be displayed publically'
        self.fields['password1'].help_text = self.fields['password1'].help_text[0:-5] + '<li>(Consider using a Password Manager like <a href="https://1password.com" class="regular" target="_blank">1Password</a> @ $2.99 a month)</li>' + '</ul>'
    
    class Meta:
        model = User
        fields = ('username', 'password1', 'password2', 'first_name', 'last_name', 'email', )        
        
        # This is not working
        help_texts = {
            'username': 'Please enter a username',
            'email': 'Your email address will not be displayed publically',
            # 'password1' : 'Please enter a password', # This is not working
            # 'password2' : 'Please enter a password', # This is not working
            }

When in doubt, follow the source.

The password1 form field in UserCreationForm has the line:
help_text=password_validation.password_validators_help_text_html(),

password_validators_help_text_html is in django.contrib.auth.password_validation.

It’s easy enough to follow from there. But these methods are not in a class that can be overridden, so changing them isn’t going to be trivial.

It looks like you would have a couple of options:

  • Edit the html list like you’re doing now.
  • Add a fake password_validator that doesn’t validate anything but has a get_help_text method.
  • Inject it in the DOM in the browser via JavaScript.
  • Edit the help_text attribute for that field to call your own function to prepare the list.

There may be more options.