连接 django 注册信号

发布于 2024-09-13 11:02:08 字数 387 浏览 3 评论 0原文

我有一个函数:

def create(sender, **kw):
  [...]

当调用 django-registration 的 user_activated 信号时应该调用该函数。

我使用以下方法连接信号和函数:

from registration.signals import user_activated
[...]
post_save.connect(create, sender=user_activated, dispatch_uid="users-atactivation-signal")

但是当用户单击通过电子邮件收到的激活链接时,不会调用该函数。

我在这里想念什么。

I have a function:

def create(sender, **kw):
  [...]

Which should be called when the user_activated signal from django-registration is called.

I connect the signal and the function using this:

from registration.signals import user_activated
[...]
post_save.connect(create, sender=user_activated, dispatch_uid="users-atactivation-signal")

But the function isn't called when a user clicks on the activation link, which he got by email.

What do I miss here.

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

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

发布评论

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

评论(4

牛↙奶布丁 2024-09-20 11:02:08

像这样的函数:

def create(sender, user, request, **kwarg):
[...]

和像这样的 connect 调用:

user_activated.connect(create)

可以完成这项工作。我的 signal.py 文件中有这些。

A function like this:

def create(sender, user, request, **kwarg):
[...]

and a connect call like this:

user_activated.connect(create)

does the job. I have these in my signals.py file.

单调的奢华 2024-09-20 11:02:08

如果 django-registration 应用程序并未实际安装,而只是复制到项目中,则侦听信号的代码将无法正确调用。

来自 django-registration v0.8 文档

我有监听注册/激活信号的函数,
但他们没有接到电话!

最常见的原因是将 django-registration 放置在
Python 导入路径上的子目录,而不是
像平常一样将其直接安装到导入路径上。导入自
在这种情况下 django-registration 可能会导致各种问题,包括
错误地连接信号处理程序。例如,如果您要
将 django-registration 放置在名为 django_apps 的目录中,并且
以这种方式引用它,你最终会遇到这样的情况:
您的代码执行以下操作:

从 django_apps.registration.signals 导入 user_registered

但是 django-registration 会做:

从registration.signals导入user_registered

从Python的角度来看
显然,这些 import 语句引用了两个不同的对象
不同的模块,因此信号处理程序连接到来自
当使用以下命令发送信号时,不会调用第一个导入
第二次导入。

要避免此问题,请遵循安装的标准做法
django-registration 直接在您的导入路径上并始终引用
通过它自己的模块名称:注册(一般来说,它是
遵循正常的 Python 实践来安装总是一个好主意
并使用 Django 应用程序)。

If the django-registration app wasn't actually installed, but rather just copied into a project your code that listens for signals won't get called properly.

From the django-registration v0.8 documentation:

I’ve got functions listening for the registration/activation signals,
but they’re not getting called!

The most common cause of this is placing django-registration in a
sub-directory that’s on your Python import path, rather than
installing it directly onto the import path as normal. Importing from
django-registration in that case can cause various issues, including
incorrectly connecting signal handlers. For example, if you were to
place django-registration inside a directory named django_apps, and
refer to it in that manner, you would end up with a situation where
your code does this:

from django_apps.registration.signals import user_registered

But django-registration will be doing:

from registration.signals import user_registered

From Python’s point
of view, these import statements refer to two different objects in two
different modules, and so signal handlers connected to the signal from
the first import will not be called when the signal is sent using the
second import.

To avoid this problem, follow the standard practice of installing
django-registration directly on your import path and always referring
to it by its own module name: registration (and in general, it is
always a good idea to follow normal Python practices for installing
and using Django applications).

瑶笙 2024-09-20 11:02:08

user_activated 本身就是一个信号。所以你必须发送带有参数的自身。除了发送者(即用户)之外,它还需要 2 个参数,请求

user_activated.send(sender=Foo, user=request.user, request=request)

Foo 是用于激活用户的后端类。

user_activated is itself is a Signal. So you have to send itself, with parameters. It requires 2 arguments apart from sender, i.e. user, request

user_activated.send(sender=Foo, user=request.user, request=request)

Foo is the backend class used to activate the user.

草莓酥 2024-09-20 11:02:08

将信号处理方法连接到该信号的代码是否确实已加载? (您可以在其后立即使用打印语句进行测试)。您可以通过从应用程序的 __init__.py 导入信号处理程序来确保为给定应用程序加载信号处理程序:

from nameofapp.nameofilewithhandlersinit import *

PS。这是dispatch_uid 中的拼写错误,还是故意的名称?

Is the code that connects the signal-handling method to that signal definitely being loaded? (You can test with a print statment immediately after it). You can make sure you load your signal handlers for a given app by importing them from that app's __init__.py:

from nameofapp.nameoffilewithhandlersinit import *

PS. is that a typo in the dispatch_uid, or a deliberate name?

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