django-registration 自动创建用户配置文件

发布于 2024-11-01 08:53:52 字数 2384 浏览 0 评论 0原文

我正在使用 django-registration,并尝试连接到其信号以自动创建用户配置文件。

信号定义:

from django.dispatch import Signal

# A new user has registered.
user_registered = Signal(providing_args=["user", "request"])

django-registration 发送的信号:

    def register(self, request, **kwargs):
    """
    Create and immediately log in a new user.

    """
    username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
    User.objects.create_user(username, email, password)

    # authenticate() always has to be called before login(), and
    # will return the user we just created.
    new_user = authenticate(username=username, password=password)
    login(request, new_user)
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user

我的信号连接:

from registration.signals import *
from core.models import UserProfile
from django.contrib.auth.models import User

def createUserProfile(sender, instance, **kwargs):
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

不用说没有创建 UserProfile。我在这里缺少什么?

多谢!

编辑:我将 connect() 及其相应的方法移动到 model.py 中,但仍然没有运气。

新代码:

from django.db import models

from django.contrib import auth
from django.contrib.auth import login
from core.forms import AuthForm
from registration.signals import *
from django.contrib.auth.models import User


# Create your models here.

class UserProfile(models.Model) :
    user = models.ForeignKey(User, unique=True)

    def __unicode__(self):
        return self.user.username


def createUserProfile(sender, instance, **kwargs):
    print "creating profile"
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

我正在使用 Pycharm 进行调试,一开始就命中了 user_registered.connect() 上的断点。所以我假设 connect() 已正确注册。但是,我仍然没有看到 createUserProfile 正在运行。我还缺少什么吗?

谢谢!

答案: Doh。我的连接和接收器代码错误。正确的代码:

def createUserProfile(sender, user, request, **kwargs):
UserProfile.objects.get_or_create(user=user)

user_registered.connect(createUserProfile)

在django-registration中阅读signals.py后意识到

I'm using django-registration and I'm trying to connect to its signals to automatically create a UserProfile.

Signal definition:

from django.dispatch import Signal

# A new user has registered.
user_registered = Signal(providing_args=["user", "request"])

Signal send by django-registration:

    def register(self, request, **kwargs):
    """
    Create and immediately log in a new user.

    """
    username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
    User.objects.create_user(username, email, password)

    # authenticate() always has to be called before login(), and
    # will return the user we just created.
    new_user = authenticate(username=username, password=password)
    login(request, new_user)
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user

My signal connect:

from registration.signals import *
from core.models import UserProfile
from django.contrib.auth.models import User

def createUserProfile(sender, instance, **kwargs):
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

Needless to say no UserProfile is being created. What am I missing here?

Thanks a lot!

EDIT: I moved my connect() and its corresponding method to a model.py and still no luck.

New code:

from django.db import models

from django.contrib import auth
from django.contrib.auth import login
from core.forms import AuthForm
from registration.signals import *
from django.contrib.auth.models import User


# Create your models here.

class UserProfile(models.Model) :
    user = models.ForeignKey(User, unique=True)

    def __unicode__(self):
        return self.user.username


def createUserProfile(sender, instance, **kwargs):
    print "creating profile"
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

I'm using Pycharm to debug, and in the very beginning my breakpoint on user_registered.connect() is hit. So I assume that connect() is being registered correctly. However, I still don't see createUserProfile being run. Anything else I'm missing?

Thanks!

ANSWER: Doh. My connect and receiver code was wrong. Correct code:

def createUserProfile(sender, user, request, **kwargs):
UserProfile.objects.get_or_create(user=user)

user_registered.connect(createUserProfile)

Realized it after I read signals.py in django-registration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱绻入梦 2024-11-08 08:53:52

您需要在服务器启动时导入的模块中注册(连接)信号。 user_registered.connect(createUserProfile, sender=User) 所在的文件很可能不会在启动时导入。来自 django 文档:

您可以将信号处理和
注册码可以放在任何你喜欢的地方。
但是,您需要确保
它所在的模块会提前导入
以便信号处理得到
在任何信号需要之前注册
被发送。这使得您的应用程序
models.py 是一个放置的好地方
信号处理程序的注册。

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

因此,自定义应用程序的 models.py 将是一个好地方(或在服务器启动时肯定导入的任何其他模块)。

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).

琉璃梦幻 2024-11-08 08:53:52

Torsten 是对的:另一种方法是使用 装饰器,如 文档

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

我喜欢这种方式,因为它紧凑且可读。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文