Django-Registration覆盖注册表

Django-Registration override Registration Form

我正在为项目使用 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 被隐藏,但其他字段呈现时没有class。我需要做什么?

我曾经遇到过同样的问题,我用过这个


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