1

This question is a follow-up to the solution explained by Muki here:

Problem in adding custom fields to django-registration

I have installed and have been successfully using the Django-registration package. By default, when you create an account using this package, it asks for your username, email address and password. I want it to also ask for (optional) first name + last name. Muki's answer at the link above explains how to do so.

However, Muki left out what should go into the file that he creates in the custom/forms.py. I need to know what the name of the class I should create in here is and what the field definitions should look like.

Can someone please post a sample forms.py that I can use to accomplish what I'm trying to do?

Community
  • 1
  • 1
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

2 Answers2

0

If your custom backend is in the custom folder, then custom/forms.py could be something like this:

from django import forms
from registration.forms import RegistrationForm


class RegistrationFormWithName(RegistrationForm):
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)

This adds your two optional fields to the default registration form. To tell your custom backend to use this new form instead of the default you need to change the get_form_class method in custom/__init__.py Muki's answer explains how to do that.

Of course, you'll also need to handle saving the data from the first_name and last_name fields.

Edit:

Your custom/__init__.py needs to look something like this:

from registration.backends.default import DefaultBackend
from registration.backends.custom.forms import RegistrationFormWithName

class CustomBackend(DefaultBackend):
    def get_form_class(self, request):
        return RegistrationFormWithName

And custom/urls.py needs a line like this:

url(r'^register/$', register, {'backend': 'registration.backends.custom.CustomBackend'}, name='registration_register'),

Change the name of the CustomBackend class in both files to whatever you're calling your new backend.

Have a look at the default backend source to get an idea of the other methods you can override. Hope that helps.

Community
  • 1
  • 1
Kevin
  • 959
  • 10
  • 14
  • I'm lost. Still see register form with only 4 fields custom.forms.py: from django import forms from registration.forms import RegistrationForm class RegistrationFormWithName(RegistrationForm): first_name=forms.CharField(required=False) last_name=forms.CharField(required=False) custom/__init.py__ includes: from registration.forms import RegistrationForm from registration.backends.custom.forms import RegistrationForm custom/urls.py, I changed line as such: url(r'^register/$', register, {'backend': 'registration.backends.custom.DefaultBackend'}, name='registration_register'), – Saqib Ali Feb 03 '13 at 05:39
0

This link explains the process well and works with django-registration 1.0

here are a few extra pointers in addition to the above code.

To update the first name change this in the models.py

def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.is_human = bool(request.POST["is_human"])
user.first_name = request.POST["firstname"]
user.save()
profile.save()

user_registered.connect(user_registered_callback)

and in the forms.py file

class ExRegistrationForm(RegistrationForm):
    is_human = forms.BooleanField(label = "Are you human?:")
    firstname = forms.CharField(max_length=30)
    lastname = forms.CharField(max_length=30)

finally to see the changes on the form create an appropriate template. The profile can be seen in the admin by creating a file called admin.py in your app and write the following code

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from prof.models import ExUserProfile

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
    model = ExUserProfile

class UserProfileAdmin(UserAdmin):
    inlines = [ UserProfileInline, ]

admin.site.register(User, UserProfileAdmin)
timberlake
  • 777
  • 6
  • 13