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

然后我用我的urls.py调用这个 url(r'^accounts/register/$', RegistrationView.as_view(form_class=MyRegistrationForm), name='registration_register'),

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

回答(1)

3 years ago

我有同样的问题,我用过这个

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