在我的应用中,我将AUTH_PROFILE_MODULE
设置为users.UserProfile
。此UserProfile有一个函数create
,当新用户注册时,应调用该函数,然后创建UserProfile条目。
根据django-registration文档,所有需要做的是在我的urls.py中设置profile_callback
条目。我看起来像这样:
url(r'^register/$', register, {'form_class': RecaptchaRegistrationForm,
'profile_callback': UserProfile.objects.create,
'backend': 'registration.backends.default.DefaultBackend',},
name='registration_register')
但是我收到了这个错误:
异常值:register()得到一个意外的关键字参数'profile_callback'
那么我必须把它放在哪里才能使它发挥作用?
答案 0 :(得分:11)
您使用的是哪种版本的django-registration?您指的是哪个版本的django-registration?我不知道这个profile_callback。
实现您正在寻找的东西的另一种方法是使用Django信号(http://docs.djangoproject.com/en/dev/topics/signals/)。 django-registration应用程序提供了一些。
实现这一目标的一种方法是在项目(或应用程序)中创建一个signals.py并连接到文档所说的信号。然后将信号模块导入 init .py或urls.py文件,以确保在项目运行时可以读取它。
以下示例使用post_save信号完成,但您可能希望使用django-registration提供的信号。
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)
答案 1 :(得分:0)
Django-registration提供了两个信号:
对于您的情况,您需要user_registered
from registration.signals import user_registered
def createUserProfile(sender, instance, **kwargs):
user_profile = UserProfile.objects.create(user=instance)
user_registered.connect(createUserProfile)
您无需创建任何单独的signals.py文件。您可以将此代码保存在任何应用的models.py中。但是,由于其配置文件创建代码,您应将其保留在profiles / models.py
中