django-registration and user profile creation
In my app i have the AUTH_PROFILE_MODULE
set to users.UserProfile
. This UserProfile has a function create
which should be called, when a new user registers, and then create the UserProfile entry.
According to the django-registration documentation all in need to do is setting the profile_callback
entry in my urls.py. Mine looks like this:
url(r'^register/$', register, {'form_class': RecaptchaRegistrationForm,
'profile_callback': UserProfile.objects.create,
'backend': 'registration.backends.default.DefaultBackend',},
name='registration_register')
but I get this error:
Exception Value: register() got an unexpected keyword argument 'profile_callback'
So where do I have to put this, to make it work?
Which version of django-registration are you using ? And which version of the django-registration are you referring to ? I didn't know about this profile_callback.
Another way to achieve what you're looking for is to use Django signals (http://docs.djangoproject.com/en/dev/topics/signals/). The django-registration application provides some.
A way to achieve that is to create an signals.py in your project (or application) and connected to signals like the documentation said. Then import the signals module into your init.py or urls.py file to be sure it'll be read when your project is run.
The following example is done using the post_save signals but you may want to use the django-registration provided ones.
from django.db.models.signals import post_save
from userprofile.models import UserProfile
from django.contrib.auth.models import User
def createUserProfile(sender, instance, **kwargs):
"""Create a UserProfile object each time a User is created ; and link it.
"""
UserProfile.objects.get_or_create(user=instance)
post_save.connect(createUserProfile, sender=User)
Django-registration provides two signals, which are:
- user_registered : Sent when registration is complete
- user_activated : Sent when user has activated his account using the activation link
For your case, you need user_registered
from registration.signals import user_registered
def createUserProfile(sender, instance, **kwargs):
user_profile = UserProfile.objects.create(user=instance)
user_registered.connect(createUserProfile)
You don't need to create any separate signals.py file. You can keep this code in models.py of any of your app. However, since its Profile creation code, you should keep it in profiles/models.py
精彩评论