django-registration auto create UserProfile

扶醉桌前 提交于 2019-12-03 03:10:45

You need to register (connect) your signal in a module which is imported on server startup. Your file where user_registered.connect(createUserProfile, sender=User)lives is mot likely not imported on startup. From the django docs:

You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.

http://docs.djangoproject.com/en/dev/topics/signals/#connecting-receiver-functions

So models.py of your custom app would be a good place (or any other module which is definitely imported on server startup).

chirale

Torsten is right: the alternative way is to use decorators as stated in documentation:

    from registration.signals import user_registered
    # ...
    @receiver(user_registered)
    def your_function_name_here(sender, user, request, **kwargs):
            # your code here
            pass

I like this way because it's compact and readable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!