Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only

时间:2016-04-04 17:37:03

标签: python django authentication registration

My username field that I have on my form using django user_auth displays : "Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only" right next to it as shown in the image below. Anyone has any idea how to hide it or why does it appear on the form?

screenshot

2 个答案:

答案 0 :(得分:4)

That text is there because the help_text for the form field has been set to that text. Also see the Creating forms from models documentation on other ways the help_text value could have been set; it could have been set on the model field, for example.

Presumably it is there because there is a the field has a form validator attached to it that only passes if the username entered matches those rules.

If you are building your form from a model, you can override just this field by setting help_texts on the form Meta class:

class YourForm(ModelForm):
    class Meta:
        model = SomeModel
        help_texts = {
            'username': None,
        }

The attribute is a mapping from field id to text; setting it to None will disable the text altogether. Take into account that any validators will still be in place however!

You could also replace the whole field:

class YourForm(ModelForm):
    username = forms.RegexField(
        label='Username', 
        max_length=30,
        regex=r'^[\w-]+$',
        error_message='This value must contain only letters, numbers, hyphens and underscores.')

    class Meta:
        model = SomeModel

But do read the note section of the Overriding default fields section of the model forms documentation.

答案 1 :(得分:2)

I solved it by adding this line in my form.

username = forms.CharField(help_text=False)

Thank you.