我是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()
答案 0 :(得分:0)
我现在将重写您的课程
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 = ''
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',]
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
确实是个好主意。
希望,对您有帮助。