如何禁用Django注册密码help_text

时间:2019-06-05 16:29:37

标签: python django

我是Django的新手,我为新用户提供了Django注册页面,我和我需要禁用密码,用户名和help_text。我已阅读过一些有关禁用help_texts的类似问题,但似乎都没有禁用密码help_texts。

这是我的代码:

class CustomUserCreation(UserCreationForm):
    email = forms.EmailField()
    class Meta:
        model = User
        fields = ['username','email','password1','password2',]
        help_texts = {
            'email' : None,
            'username' : None,
            'password1' : None,
            'password2' : None,

        }

class UpdateUser(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['email','username',]

class UpdateProfile(forms.ModelForm):
    profile_picture = forms.ImageField()
    class Meta:
        model = Profile
        fields = ['profile_picture',]

和我的初始化

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()

1 个答案:

答案 0 :(得分:0)

我现在将重写您的课程

1)

class CustomUserCreation(UserCreationForm):
    ''' avoid help_text with email, username, password1, password2 (As you want)'''
    email = forms.EmailField()

    class Meta:
        model = User
        fields = (
            'username',
            'email',
            'password1',
            'password2'
        )

    def __init__(self, *args, **kwargs):
        super(CustomUserCreation, self).__init__(*args, **kwargs)
        for field_name in ('username', 'email', 'password1', 'password2'):
            self.fields[field_name].help_text = ''

2)

class UpdateUser(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['email','username',]

class UpdateProfile(forms.ModelForm):
    profile_picture = forms.ImageField()
    class Meta:
        model = Profile
        fields = ['profile_picture',]

3)

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

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

        for field_name in ('email', 'username', 'password1', 'password2'):
            self.fields[field_name].help_text = ''
help_texts中的

class Meta属性负责override help_text中由Model生成的字段(使用ModelForm时)。在您的情况下,请使用覆盖__init__并直接读取字段(所有字段,无关紧要是从Model生成的,或者只是Form的属性都填充在self.fields实例属性中,代表OrderedDict),然后为每个字段覆盖help_text确实是个好主意。

希望,对您有帮助。