python - Django 监听器听不到信号

标签 python django django-signals

我尝试在激活时(而不是在创建用户时)保存用户配置文件,但监听器不会调用 create_user_profile()。监听器位于 models.py 中。

from registration.signals import user_activated
from mysite.signals import create_user_profile

user_activated.connect(create_user_profile, sender=User)

我正在使用 django-registration-redux。我没有覆盖注册过程中的任何内容。在 registration/signals.py 中,信号为:

user_activated = Signal(providing_args=["user", "request"])

registration/default/views.py中有这个函数:

def activate(self, request, activation_key):
    """
    Given an an activation key, look up and activate the user
    account corresponding to that key (if possible).

    After successful activation, the signal
    ``registration.signals.user_activated`` will be sent, with the
    newly activated ``User`` as the keyword argument ``user`` and
    the class of this backend as the sender.

    """
    activated_user = RegistrationProfile.objects.activate_user(activation_key)
    if activated_user:
        signals.user_activated.send(sender=self.__class__,
                                    user=activated_user,
                                    request=request)

在 PyCharm 中,我在调用 send() 的最后一行放置了一个断点。当用户被激活时,执行会在该断点处暂停,然后从那里继续执行,不会出现错误消息。就好像听众根本不存在一样。

最佳答案

发送者可能应该是 User 类:

signals.user_activated.send(sender=activated_user.__class__,
                            user=activated_user,
                            request=request)

关于python - Django 监听器听不到信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27885464/

相关文章:

python - flask-sqlalchemy 对特定表使用 drop_all 和 create_all

python - 在 dask 中为一个工作人员并行/分配子进程调用?

python - 如何使用 django 配置 apache?

django - 如何获取 django 查询的最后 n 个对象?

python - 如何对使用 SimpleITK 读取的 DICOM 图像进行直方图均衡化

python - 如何从 Google 云函数返回 JSON

python - Django 围绕标题发送电子邮件 u''

django - 如何将 Django 信号与抽象模型一起使用?

python - Django 如何使用 connection_created 信号

Django 排除模型发送信号