36

Probably a poor question, but I'm using Django's UserCreationForm (slightly modified to include email), and I would like to remove the help_text that Django automatically displays on the HTML page.

On the Register portion of my HTML page, it has the Username, Email, Password1 & Password 2 fields. But underneath Username is "Required. 30 characters or fewer. Letters, digits, and @... only." And under Password Confirmation (Password 2), it says "Enter the same password as above for verification."

How do I remove these?

#models.py
class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
        return user

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")
        exclude = ('username.help_text')

#views.py
def index(request):
    r = Movie.objects.all().order_by('-pub_date')
    form = UserCreateForm()
    return render_to_response('qanda/index.html', {'latest_movie_list': r, 'form':form},     context_instance = RequestContext(request))

#index.html
<form action = "/home/register/" method = "post" id = "register">{% csrf_token %}
    <h6> Create an account </h6>
    {{ form.as_p }}
    <input type = "submit" value = "Create!">
    <input type = "hidden" name = "next" value = "{{ next|escape }}" />
</form>
djvg
  • 11,722
  • 5
  • 72
  • 103
Chris Yin
  • 761
  • 1
  • 11
  • 23

11 Answers11

59

You can set help_text of fields to None in __init__

from django.contrib.auth.forms import UserCreationForm
from django import forms

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    def __init__(self, *args, **kwargs):
        super(UserCreateForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'password1', 'password2']:
            self.fields[fieldname].help_text = None

print UserCreateForm()

output:

<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" maxlength="30" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>

If you are doing too many changes, in such cases it is better to just override the fields e.g.

class UserCreateForm(UserCreationForm):
    password2 = forms.CharField(label=_("Whatever"), widget=MyPasswordInput 

but in your case my solution will work very well.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Throws me this error: list indices must be integers or slices, not str – Enrico Sep 09 '19 at 12:53
  • second version worked best and was very simple. `password1 = forms.CharField(label='Password', widget=forms.PasswordInput, help_text='Minimum 8 characters.')` – nihilok Nov 10 '20 at 14:29
22

Another, cleaner option is to use help_texts dictionary in class Meta. Example:

class UserCreateForm(UserCreationForm):
    ...
    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")
        help_texts = {
            'username': None,
            'email': None,
        }

More info in here: https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#overriding-the-default-fields

Works perfect for username and email, but doesn't work for password2. No idea why.

djvg
  • 11,722
  • 5
  • 72
  • 103
iustitia
  • 391
  • 3
  • 8
  • Worked for me for a ForeignKey. Thanks! – texnic Oct 24 '17 at 23:33
  • 4
    I agree that the docs say this should work, but it also has no effect for me, not even for `username` and `password1`. If anybody has more info on what's necessary to get this option to apply, I would appreciate if you could share. I debugged this for a while, but when I render this form, Django doesn't even come close to touching any of `help_texts` fields. – Henrik Heimbuerger Nov 09 '18 at 12:12
  • 2
    See the first green box ("Note") in this paragraph: https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/#overriding-the-default-fields. – Marc Jan 21 '20 at 08:21
  • Way cleaner indeed. You can also use `help_texts = {k:"" for k in fields}` to remove all the help texts – zar3bski Mar 18 '20 at 12:06
  • @HenrikHeimbuerger In the Django docs, they are subclassing `forms.ModelForm`. Here in @iustitia's answer, he is subclassing `UserCreationForm`. That might be why the docs work in that case. – Jarad Apr 11 '20 at 17:11
  • The reason it doesn't work for `password2` is that `help_text` is used for the list of password validation rules. – Salvatore Iovene Jan 24 '23 at 21:33
8

You can add css class to registration_form.html file like this.

<style>

.helptext{
  visibility: hidden;
}

</style>
6

Simple CSS solution.

<style>
     #hint_id_username, #hint_id_password1 {
         display: none;
     }
</style>

When the forms render inspect the page source code and you will see an id for each help text. Such as hint_id_username for each form field. Use the above CSS to hide the text.

kvothe__
  • 591
  • 4
  • 10
4

I had a similar issue. Based on one of the comments, here is the solution after reading the documentation.

class UserCreateForm(UserCreationForm):
    password1 = forms.CharField(label='Enter password', 
                                widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm password', 
                                widget=forms.PasswordInput)
    class Meta:
        model=User
        fields=("username","email","first_name",
                "last_name","password1","password2")
        help_texts = {
            "username":None,
        }

Basically what we are trying to do is over-ride the automated setting by re-creating the password fields for our new class form.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ajay Shah
  • 414
  • 5
  • 10
  • Here is the documentation that helped me understand the issue at hand: https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/#overriding-the-default-fields – Ajay Shah Aug 05 '20 at 13:44
1

Or just iterate through form fields and omit to ouput "field.help_text"

{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }} {{ field }}
        <!--
        {% if field.help_text %}
           <p class="help">{{ field.help_text|safe }}</p>
        {% endif %}
        -->
    </div>
{% endfor %}

Django doc: https://docs.djangoproject.com/en/3.0/topics/forms/#looping-over-the-form-s-fields

cristian
  • 518
  • 5
  • 20
1

For those who want to change the default text for a password 1 and 2 without having to recreate your default model, can try doing like i did. Just add this init function right under your class meta.

def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.fields['password1'].help_text='Your text'
      self.fields['password2'].help_text='Your text'
INGl0R1AM0R1
  • 1,532
  • 5
  • 16
0

Just go to UserCreationForm and make the required changes.

very simple hold control button on your keyboard and ckick on UserCreationForm you will get the UserCreationForm make the required changes as per your need and save it. as i did it for me in the below example i commented the Help Content.

  error_messages = {
    'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
    label=_("Password"),
    strip=False,
    widget=forms.PasswordInput,
    # help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
    label=_("Password confirmation"),
    widget=forms.PasswordInput,
    strip=False,
    help_text=_("Enter the same password as before, for verification."),
)
  • The question already has an accepted answer, please avoid answering old questions if it dosent add anything new to the existing answers. –  Feb 15 '20 at 16:40
0

Just override the field's help_text property.

For example

username = forms.CharField(help_text=None)

You don't need to change any other parameters. It will work fine.

Praveen Kumar
  • 849
  • 8
  • 8
0

If you want to remove the help text from the fields in your custom SignUpForm, you can override the init method and set the help_text attribute of each field to an empty string. Here's how you can do it:

Remove help text for all fields

    for field_name in self.fields:
        self.fields[field_name].help_text = ''
-1

Add this CSS in order to remove help text.

<style>
    .helptext {
      visibility: hidden;
    }
    body > main > form > ul > li{
      display: none;
    }
 </style>
king juno
  • 11
  • 1
  • 4