Search code examples
djangodjango-formsdjango-registration

Django-Registration override Registration Form


I'm using Django-Registration 2.3 for a project and trying to override the standard RegistrationForm with the following:

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

I'm then calling this from my urls.py with url(r'^accounts/register/$', RegistrationView.as_view(form_class=MyRegistrationForm), name='registration_register'),

In the template the captcha, first_name and last_name fields are rendered using form-control, the username is hidden but the other fields are rendered without the class. What do I need to do?


Solution

  • i had the same problem once and i used this

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