简体   繁体   中英

How to add new fields in registration form using django-registration

How to add new fields in registration form ? I had a hard time to find out so I am sharing it here

-- forms.py:

from registration.forms import RegistrationForm

class UserProfileRegistrationForm(RegistrationForm):
    first_name = forms.CharField(max_length=15, label='First name')
    last_name = forms.CharField(max_length=15, label='Last name')
    date_of_birth = forms.DateField(label='Date of birth',
                                    widget=SelectDateWidget(years=[y for y in range(1950,
                                                                                    datetime.datetime.now().year-17)],
                                                            attrs=({'style': 'width: 33%; display: inline-block;'})),)

    class Meta:
        model = UserModel()
        fields = (UsernameField(), "email", 'first_name', 'last_name',
                  'date_of_birth')

-- views.py

from registration.views import RegistrationView

class UserProfileRegistration(RegistrationView):
    success_url = '/'
    form_class = UserProfileRegistrationForm

    def register(self, form):
        """
        Implement user-registration logic here.

        """

        User = UserModel()
        user = User.objects.create_user(
            username = form.cleaned_data['username'],
            first_name = form.cleaned_data['first_name'],
            last_name = form.cleaned_data['last_name'],
            email=form.cleaned_data['email'],
            password=form.cleaned_data['password1']
        )

Note: if you want the user to login automatically use this: https://djangosnippets.org/snippets/1547/

-- urls.py

from myapp.views import UserProfileRegistration

url(r'^accounts/register/$', UserProfileRegistration.as_view(), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),

Thanks for DrJackilD. reference: https://github.com/macropin/django-registration/issues/73

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM