Django-Registration & Django-Profile, using your own custom form

You’re halfway there – you’ve successfully built a custom form that replaces the default form. But you’re attempting to do your custom processing with a save() method on your model form. That was possible in older versions of django-registration, but I can see from the fact that you specified a backend in your URL conf that you’re using v0.8.

The upgrade guide says:

Previously, the form used to collect
data during registration was expected
to implement a save() method which
would create the new user account.
This is no longer the case; creating
the account is handled by the backend,
and so any custom logic should be
moved into a custom backend, or by
connecting listeners to the signals
sent during the registration process.

In other words, the save() method on the form is being ignored now that you’re on version 0.8. You need to do your custom processing either with a custom backend or with a signal. I chose to create a custom back-end (if anyone has gotten this working with signals, please post code – I wasn’t able to get it working that way). You should be able to modify this to save to your custom profile.

  1. Create a regbackend.py in your app.
  2. Copy the register() method from DefaultBackend into it.
  3. At the end of the method, do a query to get the corresponding User instance.
  4. Save the additional form fields into that instance.
  5. Modify the URL conf so that it points to BOTH the custom form AND the custom back-end

So the URL conf is:

url(r'^accounts/register/$',
    register,
    {'backend': 'accounts.regbackend.RegBackend','form_class':MM_RegistrationForm},        
    name="registration_register"
    ),

regbackend.py has the necessary imports and is basically a copy of DefaultBackend with just the register() method, and the addition of:

    u = User.objects.get(username=new_user.username)
    u.first_name = kwargs['first_name']
    u.last_name = kwargs['last_name']
    u.save() 

Leave a Comment