django-registration和用户配置文件创建

Kai*_*Kai 7 django

在我的应用程序中我有AUTH_PROFILE_MODULE设置users.UserProfile.此UserProfile有一个函数create,当新用户注册时,应该调用该函数,然后创建UserProfile条目.

根据django-registration文档,所有需要做的是profile_callback在我的urls.py中设置条目.我看起来像这样:

url(r'^register/$', register, {'form_class': RecaptchaRegistrationForm,
'profile_callback': UserProfile.objects.create,
'backend': 'registration.backends.default.DefaultBackend',},
 name='registration_register')
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

异常值:register()得到一个意外的关键字参数'profile_callback'

那么我必须把它放在哪里才能使它工作?

Vin*_*ter 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)
Run Code Online (Sandbox Code Playgroud)

  • 看起来我使用了新的django-registration版本并阅读旧文档.我刚刚在提交消息中发现了这一点:"自定义信号现在在用户注册和用户激活时发送.之前用于类似目的的profile_callback机制已被删除,因此这是向后兼容的." 所以你的解决方案是可行的. (2认同)