连接 django 注册信号
我有一个函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的函数:
和像这样的 connect 调用:
可以完成这项工作。我的 signal.py 文件中有这些。
A function like this:
and a connect call like this:
does the job. I have these in my signals.py file.
如果 django-registration 应用程序并未实际安装,而只是复制到项目中,则侦听信号的代码将无法正确调用。
来自 django-registration v0.8 文档:
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:
user_activated
本身就是一个信号。所以你必须发送带有参数的自身。除了发送者(即用户)之外,它还需要 2 个参数,请求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, requestFoo
is the backend class used to activate the user.将信号处理方法连接到该信号的代码是否确实已加载? (您可以在其后立即使用打印语句进行测试)。您可以通过从应用程序的
__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?