Removing help_text from Django UserCreateForm

后端 未结 7 1259
感动是毒
感动是毒 2020-12-05 14:10

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

相关标签:
7条回答
  • 2020-12-05 14:51

    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.

    0 讨论(0)
提交回复
热议问题