Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-registration: How to make account creation ask for first/last name

I'm using the Django-Registration package to have users create accounts, authenticate them and log in to my webapp.

However, the form/view for account creation doesn't ask the user for a firstname/last name (those fields are part of the model BTW). It only asks for their email address, login ID and password (twice). I would like it to ask for the user's first name/last name (these fields can be optional... but it should still ask). I cannot find the form and view files to modify such that it asks for this info. I have modified the template file. But without the form and view modifications that's useless. How is this done?

like image 653
Saqib Ali Avatar asked Feb 17 '23 22:02

Saqib Ali


2 Answers

In your forms.py, extend the DjangoRegistration form like:

class MyExtendedForm(RegistrationForm):
    first_name = forms.CharField(widget=forms.TextInput(label="first_name"))
    last_name = forms.CharField(widget=forms.TextInput(label="last_name"))

In the urls.py, tell django-registration to use this extended form:

# using my registration form to override the default
(r'^register/$', 
    register, 
    {'backend': 'registration.backends.default.DefaultBackend',
     'form_class': MyExtendedForm}),

Define user_created to save the extra information:

def user_created(sender, user, request, **kwargs):
    """
    Called via signals when user registers. Creates different profiles and
    associations
    """
    form = MyExtendedForm(request.Post)
    # Update first and last name for user
    user.first_name=form.data['first_name']
    user.last_name=form.data['last_name']
    user.save()

Then, register for signals from django-registration to call your function after any registration is processed:

from registration.signals import user_registered
user_registered.connect(user_created)
like image 195
zaphod Avatar answered Apr 06 '23 22:04

zaphod


Once you have everything set up like shown here use following CustomUserForm:

class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
        fields = ['first_name','last_name','username','email','password1','password2']
like image 45
Aseem Avatar answered Apr 06 '23 23:04

Aseem