Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing help_text from Django UserCreateForm

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> 
like image 224
Chris Yin Avatar asked Nov 02 '12 20:11

Chris Yin


2 Answers

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.

like image 109
Anurag Uniyal Avatar answered Sep 18 '22 21:09

Anurag Uniyal


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/1.11/topics/forms/modelforms/#overriding-the-default-fields

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

like image 21
iustitia Avatar answered Sep 20 '22 21:09

iustitia