2009-12-15 63 views
7

在我的應用程序中,我有AUTH_PROFILE_MODULE設置爲users.UserProfile。此用戶配置文件具有功能create,應在新用戶註冊時調用該功能,然後創建UserProfile條目。django註冊和用戶配置文件創建

根據django註冊文檔,所有需要做的就是在我的urls.py中設置profile_callback條目。我的是這樣:

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

,但我得到這個錯誤:

Exception Value: register() got an unexpected keyword argument 'profile_callback'

那麼,我必須把這個,使它工作?

回答

11

您正在使用哪個版本的django註冊?你指的是哪個版本的Django註冊?我不知道這個profile_callback。

另一種實現你要找的東西是使用Django信號(http://docs.djangoproject.com/en/dev/topics/signals/)。 Django註冊應用程序提供了一些。

實現該目的的一種方法是在項目(或應用程序)中創建signals.py,並將其連接到文檔所述的信號。然後將信號模塊導入您的init .py或urls.py文件以確保在您的項目運行時讀取它。

以下示例使用post_save信號完成,但您可能想使用提供的django註冊。

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) 
+2

看起來像我用了一個新的Django註冊版本,並閱讀舊的文檔。我剛剛在提交消息中發現了這一點: 「自定義信號現在在用戶註冊和用戶激活時發送。之前用於類似目的的profile_callback機制已被刪除,因此這是向後不兼容的。 所以你的解決方案是要走的路。 – Kai 2009-12-15 19:36:14

0

Django的登記提供的兩個信號,它們是:

  • user_registered:發送當註冊完成
  • user_activated:當用戶使用所述激活鏈接
已經激活他的帳戶發送

對於您的情況,您需要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中。但是,自從它的配置文件創建代碼,你應該保留在配置文件/ models.py

相關問題