我正在使用Django-Registration 2.3进行项目并尝试使用以下内容覆盖标准RegistrationForm:
class MyRegistrationForm(RegistrationForm):
captcha = NoReCaptchaField()
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password1', 'password2']
widgets = {
'first_name' : forms.TextInput(attrs={'class' : 'form-control'}),
'last_name' : forms.TextInput(attrs={'class' : 'form-control'}),
'username' : forms.HiddenInput(),
'email' : forms.EmailInput(attrs={'class' : 'form-control'}),
'password1' : forms.PasswordInput(attrs={'class' : 'form-control'}),
'password2' : forms.PasswordInput(attrs={'class' : 'form-control'}),
}
然后我使用url(r'^accounts/register/$', RegistrationView.as_view(form_class=MyRegistrationForm), name='registration_register'),
在模板中,captcha
,first_name
和last_name
字段使用表单控件呈现,username
被隐藏,但其他字段在没有类的情况下呈现。我需要做什么?
答案 0 :(得分:1)
我曾经遇到过同样的问题而且我使用了这个
class MyRegistrationForm(RegistrationForm):
captcha = NoReCaptchaField()
first_name = forms.CharField(max_length=30, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
last_name = forms.CharField(max_length=30, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
email = forms.EmailField(max_length=254, widget=forms.TextInput(attrs={'class': 'form-control'}))
password1 = forms.CharField(max_length=30, required=True, widget=forms.TextInput(attrs={'class': 'form-control',
'name': "password1",
'type': "password"}))
password2 = forms.CharField(max_length=30, required=True, widget=forms.TextInput(attrs={'class': 'form-control',
'name': "password2",
'type': "password"}))
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email']
widgets = {
'username' : forms.TextInput(attrs = {'class': 'form-control'}),
}