在Django-Registration中注册保存个人资料

时间:2009-07-02 03:10:56

标签: django profile registration

在Django-Registration中,它表示您可以在保存用户时保存自定义配置文件 但我不知道文档要求我做什么。这是他们所说的:

  

要启用自定义用户配置文件以及User(例如,AUTH_PROFILE_MODULE设置中指定的模型)的创建,请定义一个知道如何创建和保存该模型实例的函数使用适当的默认值,并将其作为关键字参数profile_callback传递。此函数应接受一个关键字参数:

     

user

     

User将个人资料与。

联系起来

有人能举例说明需要创建的函数以及如何将其作为参数传递吗?

3 个答案:

答案 0 :(得分:8)

您可以在urls.py文件中传递回调函数。

from mysite.profile.models import UserProfile


url( r'^accounts/register/$',      'registration.views.register',
        { 'profile_callback': UserProfile.objects.create }, name = 'registration_register' ),

根据需要将您自己的函数替换为UserProfile.objects.create。

答案 1 :(得分:6)

this blogpost中介绍了这一点,并在我对another question on the same issue

的回答中进行了扩展 django-registration会在发生的各种事件中发送信号 - 注册和激活。在这些点中的任何一个点上,您都可以为该信号创建一个钩子,该钩子将被赋予用户和请求对象 - 从那里您可以为该用户创建一个配置文件。

来自django-registration的信号

#registration.signals.py 
user_registered = Signal(providing_args=["user", "request"]) 

创建个人资料的代码

#signals.py (in your project)
user_registered.connect(create_profile)

def create_profile(sender, instance, request, **kwargs):
    from myapp.models import Profile
    #If you want to set any values (perhaps passed via request) 
    #you can do that here

    Profile(user = instance).save()

答案 2 :(得分:1)

对于遇到此问题的任何人,我认为这篇博文是一个很好的教程:http://johnparsons.net/index.php/2013/06/28/creating-profiles-with-django-registration/