Django-Registration覆盖注册表

时间:2017-11-13 12:24:57

标签: django django-forms django-registration

我正在使用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'),

从我的urls.py中调用此内容

在模板中,captchafirst_namelast_name字段使用表单控件呈现,username被隐藏,但其他字段在没有类的情况下呈现。我需要做什么?

1 个答案:

答案 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'}),
        }